简体   繁体   English

在Firefox扩展中使用jQuery

[英]Using jQuery in a firefox extension

I'm trying to develop a firefox extension which draws a toolbar at the base of every webpage. 我正在尝试开发一个firefox扩展,该扩展在每个网页的底部绘制一个工具栏。

Until now i managed to make jQuery work and i proved it by running 到目前为止,我设法使jQuery正常工作,并通过运行证明了这一点。

$("body",mr.env).css("background","black"); 

in the mr.on=function() . mr.on = function()中

This code just makes the background color of the webpage black whenever i click the menu item associated with the addon. 每当我单击与插件关联的菜单项时,此代码只会使网页的背景色变黑。

But, if i try to run 但是,如果我尝试跑步

 $('body',mr.env).append( ' <img src="img/check.png" /> ' );  

it simply fails. 它只是失败了。 It doesn't show any error in Error Console and the image isn't displayed. 它在错误控制台中不显示任何错误,并且不显示图像。

Why is that? 这是为什么?

This is my overlay XUL : 这是我的叠加XUL

<script src="window.js"/>   
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.4.4.min.js"></script>

<!-- Firefox Tools menu -->
<menupopup id="menu_ToolsPopup">
    <menuitem id="menu_crypt_demo"  class="" image=""
          label="Use DnsResolver?" insertbefore="javascriptConsole" accesskey="o" 
          oncommand="DnsResolver.onMenuItemCommand(event);">
    </menuitem>
</menupopup>

This is the JavaScript file (window.js) : 这是JavaScript文件(window.js)

var DnsResolver = {
    onLoad: function() {
    // initialization code
    this.initialized = true;

},

onMenuItemCommand: function() {
testextension.on();
window.open("chrome://dnsresolver/content/window.xul", "", "chrome");
}

};


window.addEventListener("load", function(e) { DnsResolver.onLoad(e); }, false);

if(!testextension){ var testextension={};}

(function(){

var mr=testextension;


mr.on=function(){
    mr.loadLibraries(mr); 
    var jQuery = mr.jQuery; 
    var $ = function(selector,context){ return new jQuery.fn.init(selector,context||window._content.document); };
    $.fn = $.prototype = jQuery.fn;

    mr.env=window._content.document;

            /*$("body",mr.env).css("background","black");*/
    $('body',mr.env).append('<img src="img/check.png" />');


$(mr.env).ready(function(){

    // hide and make visible the show
    $("span.close a",mr.env).click(function() {
        $("#tbar"),mr.env.slideToggle("fast");
        $("#tbarshow",mr.env).fadeIn("slow");
    });

    // show tbar and hide the show bar
    $("span.show a",mr.env).click(function() {
        $("#tbar",mr.env).slideToggle("fast");
        $("#tbarshow",mr.env).fadeOut();
    });
});


    /*$("body",mr.env).css("background","black");*/
}

// Loading the Jquery from the mozilla subscript method
mr.loadLibraries = function(context){
    var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
                                                 .getService(Components.interfaces.mozIJSSubScriptLoader);
    loader.loadSubScript("chrome://dnsresolver/content/jquery-1.4.4.min.js",context);
    var jQuery = window.jQuery.noConflict(true);
    if( typeof(jQuery.fn._init) == 'undefined') { jQuery.fn._init = jQuery.fn.init; }
    mr.jQuery = jQuery;
}

})();

Starting with Firefox 3, chrome resources can no longer be referenced from within <img> , <script> , or other elements contained in, or added to, content that was loaded from an untrusted source. 从Firefox 3开始,无法再从<img><script>或从不受信任的来源加载的内容中包含或添加的其他元素中引用chrome资源。 This restriction applies to both elements defined by the untrusted source and to elements added by trusted extensions. 此限制适用于不受信任源定义的元素以及受信任扩展添加的元素。 If such references need to be explicitly allowed, set the contentaccessible flag to yes to obtain the behaviour found in older versions of Firefox. 如果需要明确允许此类引用,请将contentaccessible标志设置为yes,以获取旧版本Firefox中的行为。

Use the HTML tab in FireFox to know actually if the img element was added. 使用FireFox中的HTML选项卡可以实际知道是否已添加img元素。 It probably was added and the problem is with your URL. 它可能已添加,问题出在您的URL。

I remember when building my FireFox extensions, that files are located through a special protocol (chrome:// I think), where you put the name of the extension and can browse through it. 我记得在构建FireFox扩展时,这些文件是通过特殊协议定位的(chrome://我认为),您可以在其中放置扩展名并可以浏览它。

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

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