简体   繁体   English

如何从命令行将参数传递给我的 Javascript 代码?

[英]How can I pass an argument into my Javascript code from command line?

My JS file "login.js" can be run from the command line with "node login.js".我的 JS 文件“login.js”可以使用“node login.js”从命令行运行。 If I want to change the URL used I have to go into the file and edit the URL.如果我想更改使用的 URL,我必须将 go 更改为文件并编辑 URL。

I'd like to be able to pass the URL as an argument via the command line, eg "node login.js https://example.com" and for the code to use that URL.我希望能够通过命令行将 URL 作为参数传递,例如“node login.js https://example.com”以及使用该 ZE6B391A8D2C4D45902A23A8 的代码I assume this is fairly straightforward but I can't seem to work it out from reading about it online.我认为这很简单,但我似乎无法通过在线阅读来解决它。

const puppeteer = require('puppeteer');
const C = require('./constants');
const USERNAME_SELECTOR = '#email';
const PASSWORD_SELECTOR = '#password';
const CTA_SELECTOR = '#next';
const CTA_SELECTOR2 = '#submit-btn';


async function startBrowser() {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  return {browser, page};
}


async function closeBrowser(browser) {
  return browser.close();
}

async function playTest(url) {
  const {browser, page} = await startBrowser();
  page.setViewport({width: 1366, height: 768});
  await page.goto(url);
  await page.waitForSelector(USERNAME_SELECTOR);
  await page.click(USERNAME_SELECTOR);
  await page.keyboard.type(C.username);
  await page.click(CTA_SELECTOR);
  console.log('before waiting');
await delay(10000);
console.log('after waiting');
  await page.click(PASSWORD_SELECTOR);
  await page.keyboard.type(C.password);
  await page.click(CTA_SELECTOR2);
  await page.waitForNavigation();
  await page.screenshot({path: 'screenshot.png'});
}

(async () => {
  await playTest("https://example.com");
  process.exit(1);
})();

function delay(time) {
  return new Promise(function(resolve) { 
      setTimeout(resolve, time)
  });
}

In many programs (including node), arguments can be passed to your script through something we call flags.在许多程序(包括节点)中,arguments 可以通过我们称为标志的东西传递给您的脚本。

Node itself is a program and when you write node foo.js , foo.js itself is an argument to the node program. Node 本身是一个程序,当您编写node foo.js时, foo.js本身就是 node 程序的参数。 To pass arguments through command line use flags via double dash.要通过命令行传递 arguments,请通过双破折号使用标志。

Example:例子:

node foo.js bar/baz/index.html 

now the value can be accessed through process.argv which is an array of all the arguments passed to it.现在可以通过process.argv访问该值,它是传递给它的所有 arguments 的数组。 It will look something like this它看起来像这样

['node.exe', 'foo.js', 'bar/baz/indez.html']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 npm 在命令行中传递参数并在反应 javascript 代码中读取它 - How to pass argument in command line with npm and read it in react javascript code 如何将转义的javascript字符串作为参数传递给Windows命令行? - How can I pass an escaped javascript string as an argument into windows command line? 如何使用 Javascript 在终端命令行中传递 arguments? - How can I pass arguments in the terminal command line using Javascript? 如何使用命令行美化 JavaScript 代码? - How can I beautify JavaScript code using Command Line? 如何仅从命令行检查 JavaScript 代码的语法错误? - How can I check JavaScript code for syntax errors ONLY from the command line? 如何将从JSON文件读取的数据传递给其余的Javascript代码? - How can I pass data read from a JSON file to the rest of my Javascript code? 如何在NodeJS中传递命令行参数? - How to Pass a Command Line Argument in NodeJS? 如何将命令行参数传递给嵌套脚本? - How to pass a command line argument to a nested script? 如何将变量从按钮的onclick传递到javascript代码? - How can I pass variable from the onclick of a button to the javascript code? 如何从这行代码显示我的数组? - How can I display my array from this line of code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM