2010-09-08 61 views
2

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

而且它不工作... 我在做什麼錯?

+0

你正在追加腳本並立即刪除它,我懷疑這可能是一個原因。 – Neutralizer 2010-09-09 14:59:10

+0

也請確定你的函數「contentEval」是否工作正常。 – Neutralizer 2010-09-09 15:00:00

回答

4

如果toAlert恰好在頁面的全局範圍內定義,那麼您的腳本就可以工作。

在Chrome中,擴展/ Greasemonkey JavaScript不能與頁面JavaScript共享變量或閉包。
這就是爲什麼你不能直接注入該函數,從擴展範圍到頁面範圍,但必須從源字符串重新創建它。

這意味着,如果你在頁面範圍內創建一個函數,任何變量或函數,你的功能要求必須:

  1. 已經存在,全球範圍內,在源頁面。
  2. 也可以通過腳本進入源頁面。

例如,修改你的代碼是這樣的...

//-- Must recreate the variable that the function requires. 
scriptStr = 'var toAlert="' + toAlert +'";'; 

//-- Now the function. 
scriptStr += '(' + source.toString() + ')();' 

var script = document.createElement('script'); 
script.textContent = scriptStr; 

...作品,但這種做法顯然變得混亂。 (A)將所有JavaScript保留在擴展名中;不要與頁面的JavaScript進行交互。 (B)如果您必須與頁面的JS進行交互,或者像jQuery一樣加載庫,則將代碼的所有放在一個main()函數中,並將其編寫到源頁面中。

像這樣:

function localMain() 
{ 
    /*--- Put EVERYTHING inside this wrapper, functions and variables. 
     Call or use nothing else that's defined in the GM script here. 
     Can use objects in the source page's scope, though. 
    */ 
} 

//--- Now create the function in the page's scope and run it. 
var scriptNode   = document.createElement ("script"); 
scriptNode.textContent = localMain.toString() + "\n localMain();"; 
document.head.appendChild (scriptNode); 

請注意,如果您還裝載庫到頁面的範圍,那麼你可能需要通過使用定時器並檢查該庫延遲運行localMain()

+0

謝謝,我已經使用了第一個選項,它工作的很棒! – ManIkWeet 2010-09-10 07:10:29

+0

不客氣。樂意效勞。 – 2010-09-10 08:18:41

相關問題