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.
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.
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.
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
.
puppeteer
or jsdom
(detailed above).mkdir javascript && cd javascript
npm
(the node package manager): npm init -y
puppeteer
:
npm install puppeteer
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();
})();
node index.js
jsdom
:
npm install jsdom
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!'
node index.js
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.
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());
}()
;
}())
Basically scratchpad use for doing JAVASCRIPT and DOM operations.You can not run it from unix command.
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
© 2022 - 2024 — McMap. All rights reserved.