有一个页面http://example.com/1.php像往常一样包含javascript文件:
<script type="text/javascript" src="/util.js?1354729400"></script>
Run Code Online (Sandbox Code Playgroud)
这个文件包含名为exampleFunction的函数,我需要在我的用户脚本中使用它.我还有一个用户脚本:
// ==UserScript==
// @name SomeName
// @namespace http://example.com/userscripts
// @description Greets the world
// @include http://example.com/*
// ==/UserScript==
window.onload = function () {
console.log(exampleFunction);
alert("LOADED!");
}
Run Code Online (Sandbox Code Playgroud)
在Firefox中完美运行并在Chrome中返回错误:
Uncaught ReferenceError: exampleFunction is not defined
Run Code Online (Sandbox Code Playgroud)
我如何使其工作?
javascript greasemonkey google-chrome cross-browser userscripts
我现在正在摆弄chrome中的用户脚本,所以请忍受我潜在的无知/白痴.
在我正在编写脚本的页面中,有一个<script>声明变量的元素x.这是否意味着,在我的用户脚本中,我只能x从全局命名空间访问?
例如,如果我的用户脚本中的唯一一行是alert(x);,那应该按预期工作(假设x是一个字符串)?我知道chrome不支持unsafewindow,但由于某种原因,我发现无法弄清楚如何模仿功能.它甚至可能吗?
我知道Greasemonkey脚本会自动包装在以某种方式隔离的匿名函数中,以防止它们与页面中的脚本冲突.
Chrome用户脚本也会出现同样的情况吗?
javascript greasemonkey google-chrome anonymous-function userscripts
我想使用用户脚本在站点中加载另一个脚本文件.但是,js.onload事件无法正常工作.
用户脚本文件:
// ==UserScript==
// @name Code highlight
// @description Test
// @include http://localhost/*
// @version 1.0
// ==/UserScript==
var js = document.createElement('script');
js.src = "http://localhost/test/js/load.js";
document.getElementsByTagName("head")[0].appendChild(js);
js.onload = function(){
console.log(A)
}
Run Code Online (Sandbox Code Playgroud)
load.js文件:
var A = {
name:'aa'
}
Run Code Online (Sandbox Code Playgroud)
在Chrome中,控制台输出"undefined",但load.js已完全加载.
我在Firefox中进行了测试,输出A正确.