0

My school blocked CTRL + U, but you can use 'view-source:' before a link to view the code. It takes awhile, so i've been trying to make a script to automatically direct to the source code. However, I keep getting errors because it is not a link

I have tried the following:

var code = fetch(`view-source:https://${location.hostname}${location.pathname}`);
location.href = (code);

and

var code = (`view-source:https://${location.hostname}${location.pathname}`);
location.href = (code);

In the first one, I see a bad request, and in the second, I a blank page with the words "view-source:" followed by the link

1
  • fetch() doesn't return a URL you can assign to location.href. It returns a promise, which you can use to get the contents of the web page. Commented Feb 13, 2023 at 18:47

2 Answers 2

1

view-source: isn't a real protocol you can fetch().

However, just

var resp = await fetch('http://...');
var text = await resp.text();
document.body.textContent = text;

should replace the current document's body with the text contents of that URL...

Sign up to request clarification or add additional context in comments.

Comments

1

If you try from frontend to fetch the source code you will run to CORS Problems. But you can use some proxyies like in the example beloow:

fetch('https://api.codetabs.com/v1/proxy?quest=https://stackoverflow.com/questions/75440023/script-to-get-source-code-of-website-js#75440023').then((response) => response.text()).then((text) => console.log(text));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.