我该如何在没有浏览器的情况下执行JavaScript命令?

6

我该如何无头运行JavaScript?

我正在寻找像Firefox中的“Scratchpad”一样的选项,可以在没有浏览器的情况下运行JavaScript,并对特定网站进行DOM操作,最好可以从命令行执行。


3
你考虑过 https://github.com/nodejs/node 吗? - Zombo
2
这并不完全远离您可能考虑的“浏览器”,但它可能会满足您的需求。请参见PhantomJS - Scriptable Headless Browser - Booboo
4个回答

5
我找到了一篇类似的文章,其中有一个相对较新的答案可以回答你的问题 - 如何在命令行中运行基于DOM的JavaScript
主要问题是浏览器实际上是创建您需要进行交互的DOM的工具。因此,在没有任何运行“浏览器”的情况下,我们无法使用DOM。但是有一些可用的工具(如Selenium),可以自动化/模拟启动浏览器、创建DOM并运行您的JavaScript。

1
我在这里看到了很多提及 Scratchpad 的内容(以及类似 Scratch JS 的替代方案),但您的答案明确要求另一种选择。Node.js 可以很好地完成此任务,但默认情况下无法操作 DOM。此时有两个选项可供选择,一个称为 jsdom,它允许您从给定源代码操作 DOM。由于您说您正在寻找像 Scratchpad 这样的解决方案,puppeteer 也可能适用。它使用无头 Chromium 并将让您以编程方式控制 Chromium。我认为您需要 puppeteer,但从技术上讲,它使用了浏览器,因此您可能需要 jsdom

步骤:

  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.

0

如果你正在使用 Google Chrome 浏览器,你需要安装一个名为 Scratch JS 的扩展程序。它可以在浏览器的应用商店中找到。如果你想逐行运行你的代码,你可以通过在电脑上按下 Ctrl + Shift + I 进入检查模式,然后进入控制台来实现逐行运行 JavaScript 代码。如果你想要运行整个程序,你必须将 Scratch JS 安装为一个扩展程序。


2
没有浏览器。 - General Grievance

0

它无法从Unix命令行运行。您需要从Firefox提供的Scratchpad中运行它。

只要您的网站在Firefox中打开,它就会始终执行。

  1. 打开www.google.com
  2. Shift + F4打开Scratchpad

编写以下代码

(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. 按下CTRL + R

Scratchpad主要用于执行JavaScript和DOM操作,无法从Unix命令行运行。

enter image description here


3
海报已经提到他们熟悉Scratchpad,但特别想要针对CLI而非浏览器的东西。 - old greg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接