[英]The equivalent to a content script in a Firefox extension?
I am quite new to Firefox and its extensions. 我对Firefox及其扩展很新。 Is there something like a script using JavaScript that runs every time a page loads?
有没有类似于使用JavaScript的脚本,每次加载页面时都会运行? My chrome extension injects a css file creating a link tag using js.
我的chrome扩展插入了一个css文件,使用js创建了一个链接标记。 How do I port this to Firefox?
如何将其移植到Firefox?
You can use mozIJSSubScriptLoader to execute the JavaScript (chrome://myExtension/content/script.js) every time a webpage loads . 每次加载网页时,您都可以使用mozIJSSubScriptLoader来执行JavaScript (chrome://myExtension/content/script.js) 。 Here is the sample code that you need to modify according to your chrome directory
以下是您需要根据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
}
};
I used to use Greasemonkey on Firefox. 我以前在Firefox上使用Greasemonkey。 In fact, Chrome Extensions began as a mimic of Greasemonkey, and were the origin of content scripts.
事实上,Chrome Extensions最初是作为Greasemonkey的模仿,并且是内容脚本的起源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.