我有一個腳本,在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