請參閱"Accessing Variables (and Functions) from Greasemonkey to Page & vice versa。
Userscripts從目標頁面分開,所以你不能只是簡單地覆蓋函數和變量不理解上下文...
IF功能是全球(看起來像它可能是在這種情況下),你可以inject新的功能定義:
function StartDrawing() {
// WHATEVER YOU WANT FOR THE NEW CODE GOES HERE.
}
addJS_Node (StartDrawing);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement ('script');
if (runOnLoad) {
scriptNode.addEventListener ("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
這個工程幾乎支持userscripts每一個瀏覽器。
在某些情況下,您可能需要通過觸發窗口load
事件和/或檢查功能是否已存在來延遲負載。
根據您的瀏覽器,你可以有更多的選擇(見鏈接的Q & A,上圖):
- 在的Firefox + Greasemonkey的,您可以使用
@grant none
模式或unsafeWindow
。
- 在Chrome + Tampermonkey,通常可以使用
unsafeWindow
。 @grant none
模式可能起作用,但我自己沒有測試過。
如果函數不是全局:
然後在火狐因爲它來自於你必須覆蓋JS源見"Stop execution of javascript function (client side) or tweak it"。
在Chrome中,你大多運氣不佳(上次驗證六個月前)。
只需再次定義它 – exussum