Ask coding questions

← Back to all posts
How to run a JavaScript code on another website?
Whacko (205)

How do you run a JavaScript code on another website, without opening the tab?

xxpertHacker (388)

What is your goal? Are you trying to run your code against their HTML?

You could fetch their site and use a DOMParser against the text.

const document = new DOMParser().parseFromString(
    await (
        await fetch(

Are you trying to insert a script into their site?
You can't.

Are you trying to execute a script on their site? Just use the console.

This is pretty vague, why don't you want to open the tab?

ironblockhd (369)

This is prevented by the same origin policy. If it was possible, you could run code on google that steals your authentication cookie

xxpertHacker (388)

@ironblockhd Eh, you could use iframes and access their site, you could use and get access to their web page and JS window object, you could fetch their site in coors mode.

Also, many websites don't send security headers that help, my servers always do, but most Replers sure don't, and a few companies don't.

Coder100 (8396)

not possible. The closest would be visiting the website itself, use puppeteer

smh use their API

Whacko (205)

@Coder100 It just says this:

node v12.16.1
 Updating package configuration

--> npm init -y
Wrote to /home/runner/ReplBot/package.json:

  "name": "ReplBot",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "selenium-webdriver": "^4.0.0-alpha.7"
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [],
Promise { <pending> }
Hint: hit control+c anytime to enter REPL.
(node:161) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
/home/runner/ReplBot/node_modules/puppeteer/.local-chromium/linux-800071/chrome-linux/chrome: error while loading shared libraries: cannot open shared object file: No such file or directory


    at onClose (/home/runner/ReplBot/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:193:20)
    at Interface.<anonymous> (/home/runner/ReplBot/node_modules/puppeteer/lib/cjs/puppeteer/node/BrowserRunner.js:183:68)
    at Interface.emit (events.js:327:22)
    at Interface.EventEmitter.emit (domain.js:483:12)
    at Interface.close (readline.js:416:8)
    at Socket.onend (readline.js:194:10)
    at Socket.emit (events.js:327:22)
    at Socket.EventEmitter.emit (domain.js:483:12)
    at endReadableNT (_stream_readable.js:1220:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)
(node:161) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see (rejection id: 1)
(node:161) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

How do you use it?

Coder100 (8396)

oh lol I assumed you would know
ok read my post here (more like fork it)

Whacko (205)

Well, if I knew, I wouldn't be asking you for help @Coder100.

SixBeeps (3004)

Running JS code on a site without having it open is like trying to open a lock without the lock existing. Could you maybe be a bit more specific on what you're trying to achieve?

Whacko (205)

@SixBeeps Well, I want to do something on a website using JavaScript, like leave a comment or something. I'm not sure if it's possible.

Whacko (205)

@SixBeeps Or, like, go to the website and then run a JavaScript code, then close the tab.

SixBeeps (3004)

@JavaMaster1 You could use an implementation of a webdriver. There are a few drivers you can choose from, and they'll span across many languages.

Whacko (205)

@SixBeeps But how do I add it to my code?

SixBeeps (3004)

@JavaMaster1 Well it depends on what webdriver and what language you're using.

Whacko (205)

@SixBeeps I'm thinking about using HTML, or if it works better Node.js.

SixBeeps (3004)

@JavaMaster1 You won't be able to do it with just HTML and JS, so you'll need to make a Node application to do it. Looks like Selenium is on NPM, so I'd recommend you give that a try.

Whacko (205)

@SixBeeps K. I thought I would need Node.js

Coder100 (8396)

ahhh puppeteer is better :( @SixBeeps

Whacko (205)

@Coder100 @SixBeeps thanks for all these ways, I'll see which one works better and see who gets the cycles!

SixBeeps (3004)

@Coder100 Haven't used it, what does it do that Selenium doesn't?

Coder100 (8396)

@SixBeeps argh
fair enough

I think it has something about pdfs