回答

7

您需要pageMod API。

var pageMod = require("page-mod"); 
pageMod.PageMod({ 
    include: "*.example.org", 
    contentScript: 'window.alert("Page matches ruleset");' 
}); 
+1

在什麼頁面上放置這個? browser.xul中包含的js文件也許? – 2012-02-28 18:06:37

+0

@JacquesBlom:將其放入'lib/main.js'中,以便使用[Add-on SDK](https://addons.mozilla.org/en-US/developers/builder)構建的擴展。 – 2012-02-28 18:21:19

+0

當我這樣做:include:「facebook.com」 - 它不會工作 – 2012-02-28 20:13:21

0

您可以使用mozIJSSubScriptLoader來執行JavaScript 每次webpage loads時間(鉻//myExtension/content/script.js)。下面是你需要根據自己的chrome目錄

window.addEventListener("load", function load(event){ 
    window.removeEventListener("load", load, false); //remove listener, no longer needed 
    myExtension.init(); 
},false); 

var myExtension = { 
    init: function() { 
    var appcontent = document.getElementById("appcontent"); // browser 
    if(appcontent){ 
     appcontent.addEventListener("DOMContentLoaded", myExtension.onPageLoad, true); 
    } 
    var messagepane = document.getElementById("messagepane"); // mail 
    if(messagepane){ 
     messagepane.addEventListener("load", function(event) { myExtension.onPageLoad(event); }, true); 
    } 
    }, 

    onPageLoad: function(aEvent) { 
    var doc = aEvent.originalTarget; // doc is document that triggered "onload" event 
    // do something with the loaded page. 
    // doc.location is a Location object (see below for a link). 
    // You can use it to make your code executed on certain pages only. 
    var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].getService(Components.interfaces.mozIJSSubScriptLoader); 
       loader.loadSubScript("chrome://myExtension/content/script.js", doc); 

    // add event listener for page unload 
    aEvent.originalTarget.defaultView.addEventListener("unload", function(event){ myExtension.onPageUnload(event); }, true); 
    }, 

    onPageUnload: function(aEvent) { 
    // do something 
    } 
}; 
0

我以前在Firefox使用的Greasemonkey修改示例代碼。事實上,Chrome擴展開始是爲了模仿Greasemonkey,並且是內容腳本的起源。

相關問題