2009-10-19 86 views

回答

9

你想要什麼類型的調試?像Alex說的那樣,用戶腳本將與調試頁面本身在相同的上下文中列出。如果您轉到開發人員工具中的「腳本」選項卡,則會看到一個帶有下拉列表的欄,它將允許您選擇想要調試的相應JavaScript文件。這樣的腳本應該有看起來像chrome-extension://<hash>/<script file>.js的網址。這些腳本也將登錄到他們所嵌入的頁面的控制檯。

此外,如果您想要登錄所有頁面的相同位置,則可以使用用戶腳本作爲內容腳本,將腳本構建爲完整的擴展名。然後,您可以將內容腳本中的消息發送到您的背景頁面並在那裏登錄。例如,如果這是你的內容的腳本:

function log(text) { 
    chrome.extension.sendRequest({'action' : 'log', 'text' : text}, function() {}); 
}; 
log("Content script loaded: " + window.location.href); 

這是你的背景頁:

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
    <body> 
    <script> 
     function onRequest(request, sender, callback) { 
     if (request.action && request.action == 'log') { 
      console.log(request.text); 
     } 
     }; 

     chrome.extension.onRequest.addListener(onRequest); 
    </script> 
    </body> 
</html> 

你會看到在後臺頁面的日誌內容腳本的每個負載。

0

您可以使用較小的腳本實際將自定義調試腳本插入到頁面中。此時,您將在開發人員工具中擁有相同的訪問權限,就好像它實際上包含在頁面中一樣。

+1

LOL ...這基本上是我用我的用戶腳本,它不工作:) – d34dh0r53

3

我用戶在我的腳本跨瀏覽器通用API兼容性以下功能:

function testGM() { 
var isGM = typeof GM_getValue != 'undefined' && typeof GM_getValue('a', 'b') != 'undefined'; 
if(typeof(unsafeWindow) == 'undefined') { unsafeWindow = window; } 
if(!isGM) { log = function(msg) { try { unsafeWindow.console.log(msg); } catch(e) {} }; } else { log = GM_log; } 
if(window.opera) log = opera.postError; 
setValue = isGM ? GM_setValue : function (name, value) { return localStorage.setItem(name, value) }; 
getValue = isGM ? GM_getValue : function(name, def){ var s = localStorage.getItem(name); return s == null ? def : s }; 
} 
testGM(); 

它不是我的。這是禮貌sizzemctwizzle @ userscripts.org

我只使用日誌,getValue & setValue截至目前,因此只在這個函數的樹。
你也可以看看他的guide
或者您也可以結賬GIJoe'scross-browser GM Api

+0

這可能是過時的。 'typeof GM_getValue('a','b')!='undefined''在我的用戶腳本中的chrome中評估爲false –

相關問題