【问题标题】:How to set variables in a js file and include it to a userscript如何在 js 文件中设置变量并将其包含到用户脚本中
【发布时间】:2018-07-14 10:57:21
【问题描述】:

我想在一个名为 vars.js 的 js 文件中设置变量

将 vars.js 上传到我的 ftp 服务器并尝试通过使用将其包含到我的用户脚本中;

//@require http://serverip/vars.js

当我想从 vars.js 访问变量、函数等时

vars.js:

var a = function(b){
 alert(b);
 }

////
用户脚本;

...
//@require http://serverip/vars.js
a("something to alert here");

基本不起作用,报错:

ERROR: Execution of script 'New userscript' failed! a is not a function

我怎样才能让它工作?

【问题讨论】:

  • 你在使用 CommonJS (Node.js) 吗?
  • 没有常规的 js,你在 html 中在

标签: javascript variables greasemonkey userscripts tampermonkey


【解决方案1】:

您可以尝试导入命令,例如:

// ES6 syntax
import * as A from './A.js';
const foo = A.foo;
const baz = A.baz;

【讨论】:

  • 解析错误:“import”和“export”可能只出现在“sourceType:module”中
猜你喜欢
  • 1970-01-01
  • 2013-07-05
  • 2018-10-06
  • 1970-01-01
  • 2017-01-08
  • 2017-02-02
  • 2017-01-02
  • 1970-01-01
  • 2017-05-31
相关资源
最近更新 更多