简体   繁体   English

如何制作一个简单的firefox插件,该插件仅在每个新页面上执行独立的JS脚本?

[英]How to make a simple firefox addon, which just executes stand-alone JS script on every new page?

I'm trying to make a simple firefox add-on, executing a simple stand-alone JS-script on every new page (say, just a simple alert after page loaded). 我正在尝试制作一个简单的firefox插件,在每个新页面上执行一个简单的独立JS脚本(例如,页面加载后只是一个简单的警报)。

First, I've tried add-on sdk. 首先,我尝试了附加SDK。 It have been installed successfully, run tests, but was not able to execute even examples from any tutorial, so i tried to try XUL. 它已成功安装,运行测试,但甚至无法从任何教程中执行示例,因此我尝试尝试XUL。

I downloaded 'xulschoolhello.xpi', unpacked it, modified content/browserOverlay.xul to this: 我下载了“ xulschoolhello.xpi”,将其解压缩,将内容/browserOverlay.xul修改为:

<?xml version="1.0"?>    

<!DOCTYPE overlay SYSTEM
  "chrome://xulschoolhello/locale/browserOverlay.dtd">

<overlay id="xulschoolhello-browser-overlay"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/x-javascript"
    src="chrome://xulschoolhello/content/browserOverlay.js" />
</overlay>    

and content/browserOverlay.js to this just to see if something happens: content / browserOverlay.js只是为了看看是否发生了什么:

window.alert(123)    

but nothing happens after zip packing, installation and rebooting. 但是在压缩打包,安装并重新启动后,什么也没有发生。

I'm quiet new to firefox extensions, so thanks for any help. 我对Firefox扩展很陌生,因此感谢您的帮助。


UPD. UPD。

I tried to make a very simple bootstrap.js : 我试图制作一个非常简单的bootstrap.js

var WindowListener = {
    onOpenWindow: function(window) {
        window.alert(123)
    }
}

function startup(data, reason) {
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    .getService(Components.interfaces.nsIWindowMediator);

    wm.addListener(WindowListener);
}

function shutdown(data, reason) {}
function install(data, reason) {}
function uninstall(data, reason) {}

But it doestn alert. 但是,它并没有提高警惕。 What am i doing wrong here? 我在这里做错了什么?

This is a fully working bootstap addon that runs javascript everytime a url matching hostPatern of bing.com is loaded. 这是一个完全正常工作的bing.com程序加载hostPaternbing.com加载hostPaternbing.com匹配的URL时都运行javascript。 Download the xpi from this gist here and install it to see it working: https://gist.github.com/Noitidart/9287185 从此处下载该要点的xpi并进行安装以查看其工作原理: https : //gist.github.com/Noitidart/9287185

If you want to use this for yourself, then just edit the addDiv and removeDiv functions on the js you want to run. 如果您想自己使用它,则只需在要运行的js上编辑addDivremoveDiv函数。 And to control which site edit hostPattern global var and decide if you want to listen to frames by setting global var of ignoreFrames . 而控制其网站的编辑hostPattern全局变量,并决定是否要通过设置全局变量来听帧ignoreFrames

To accomplish this with addon-sdk you do it like this: 要使用addon-sdk完成此操作,您需要这样做:

var pageMod = require("page-mod");
const data = require("self").data;


exports.main = function() {
  pageMod.PageMod({ 
    include: ["https://www.bing.com/*","http://www.google.com/*"],
    contentScriptWhen: 'ready',
    /*contentScriptFile: [data.url("jquery.js"),data.url("script.js")],*/
    onAttach: function(worker) {
       console.log('loaded a page of interest');
    }
});

I'm not too familiar with addon-sdk like i dont know how to set up the environment, but i heard once you get it setup its pretty simple. 我对addon-sdk不太熟悉,就像我不知道如何设置环境一样,但是我听说一旦安装它就非常简单。 As you can see by comparing the amount of code. 通过比较代码量可以看到。 But in the bootstrap version you have fine control over everything thats why I prefer bootstrap, and most of the codes are cookie-cutter copy and paste. 但是在引导程序版本中,您可以很好地控制所有内容,这就是为什么我更喜欢引导程序的原因,并且大多数代码都是用cookie剪切程序复制和粘贴的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM