How can I execute JavaScript commands without a browser?
Asked Answered
B

4

6

How can I run JavaScript headlessly?

I am looking for an option like Scratchpad (present in Firefox) that could run JavaScript and make DOM operations on specific websites without a browser and preferably executed from the command line.

Blakeney answered 17/6, 2018 at 12:47 Comment(2)
Have you considered github.com/nodejs/nodeLoper
This is not getting totally away from what you might consider a "browser", but it might serve your purpose. See PhantomJS - Scriptable Headless Browser.Forestforestage
P
5

I found a similar post that has a relatively up to date answer for your question - Run DOM-based Javascript from command line

The main issue is that the browser is actually what creates the DOM that you want to interact with. So without some sort of "browser" running, we have no DOM. But there are some tools available (like Selenium) that can automate/simulate firing up a browser, creating the DOM, and running your javascript.

Pandowdy answered 30/9, 2020 at 19:49 Comment(0)
C
1

I've seen scratchpad mentioned a lot here (and alternatives like scratch js), but your answer specifically asks for an alternative. Node.js will work well for this purpose, but can't, by default, manipulate the DOM. There are two options for packages that do a slightly different job at this point. One is called jsdom, and allows you manipulate the DOM from given source code. Since you said you were looking for a solution like scratchpad, puppeteer might also work. It uses headless chromium and will let you control chromium programatically. I think you want puppeteer, but that technically uses a browser, so you might want jsdom.

Steps:

  1. Download and install nodejs from https://nodejs.org/
  2. Choose if you want to use puppeteer or jsdom (detailed above).
  3. Make a directory for your project and change into it: mkdir javascript && cd javascript
  4. Initialize the directory with npm (the node package manager): npm init -y
  5. If you choose puppeteer:
    • Install puppeteer: npm install puppeteer
    • Create and edit index.js to have the following:
      const puppeteer = require('puppeteer');
      
      (async () => {
        const browser = await puppeteer.launch();
      
        const page = await browser.newPage();
        await page.goto('https://example.com');
      
        await page.addScriptTag({content:`
          document.querySelector('div>h1').textContent = 'Example Puppeteer'
        `})
        await page.evaluate(() => {
          document.querySelector('div>p').textContent = "Just another way to do things."
        });
        // this ^ is slightly different because in the
        // first the javascript is being injected as a
        // script tag, while in the second the div>p is
        // actually being manipulated. The first would
        // probably be closer to scratchpad though.
      
        await page.screenshot({path: 'screenshot.png'});
        await browser.close();  
      })();
      
    • Run node index.js
    • The screenshot at screenshot.png should be of the manipulated DOM
  6. If you choose jsdom:
    • Install jsdom: npm install jsdom
    • Create and edit index.js:
      const { JSDOM } = require('jsdom')
      const { document } = (new JSDOM(`<!DOCTYPE html><html><head></head><body><p>Hello</p></body></html>`)).window
      document.querySelector('p').textContent = 'Hello World!'
      console.log(document.querySelector('p').textContent) // Will output 'Hello World!'
      
    • Run node index.js
    • You should see "Hello World!" in your console.
Cydnus answered 2/10, 2020 at 1:20 Comment(0)
W
0

It will not run from unix command line.You need to run it from Scratchpad which is provided by firefox.

It will always execute when your website is open in Firefox.

  1. Open www.google.com
  2. Shift + F4 for open scratch pad

Write Below Code

(function () {
  var
  getRandomValue = function () {
    var
    randomValue = Math.floor(Math.random() * (Math.pow(2, 16)))
    ;
    return randomValue
  },
  setFieldsToRandomValue = function () {
    document.getElementById('lst-ib').value = 'TEST' + (getRandomValue());

  }()
  ;
}())
  1. Press CTRL + R

Basically scratchpad use for doing JAVASCRIPT and DOM operations.You can not run it from unix command.

enter image description here

Wellknown answered 19/7, 2018 at 12:24 Comment(1)
The poster already mentioned that they are familiar with Scratchpad but specifically want something for the CLI, not in browser.Serrano
A
0

if you are using google chrome you need to install an extension named scratch js it will be available there and if you want to run your code line by line you could inspect by cntrl + shift + i in your computer and go to console if you want to run javascript line by line . if you wan to run the whole program you have to install scratch js as an extension

Archiphoneme answered 1/10, 2020 at 11:3 Comment(1)
"Without a browser."Vittoria

© 2022 - 2024 — McMap. All rights reserved.