我有一个脚本,在FireFox中使用unsafeWindow,因为这没有奏效,我搜索了另一个选项,并发现它,我只是想知道:我怎样才能将我的脚本中的变量用于unsafeWindow解决方法?使用userscript中的变量在Google Chrome页面中注入JS?
我的代码是:
// ==UserScript==
// @name Test
// @description Test
// @include http://www.google*
// ==/UserScript==
var toAlert = "This is what I want to alert...";
alert("Before implementation...");
contentEval(function(){ alert(toAlert);});
alert("And after...");
function contentEval(source) {
// Check for function input.
if ('function' == typeof source) {
// Execute this function with no arguments, by adding parentheses.
// One set around the function, required for valid syntax, and a
// second empty set calls the surrounded function.
source = '(' + source + ')();'
}
// Create a script node holding this source code.
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = source;
// Insert the script node into the page, so it will run, and immediately
// remove it to clean up.
document.body.appendChild(script);
document.body.removeChild(script);
}
而且它不工作... 我在做什么错?
你正在追加脚本并立即删除它,我怀疑这可能是一个原因。 – Neutralizer 2010-09-09 14:59:10
也请确定你的函数“contentEval”是否工作正常。 – Neutralizer 2010-09-09 15:00:00