简体   繁体   English

为什么我的Firefox扩展程序不使用从Firefox 4的网页中提取的jQuery 1.4.2对象添加事件处理程序

[英]Why does my Firefox extension not add event handlers using jQuery 1.4.2 object pulled from webpage in Firefox 4

My Firefox extension grabs a jQuery 1.4.2 object that is already embedded on a webpage and then tries to use that jQuery object to modify that page. 我的Firefox扩展捕获了一个已经嵌入在网页中的jQuery 1.4.2对象,然后尝试使用该jQuery对象来修改该页面。 It worked well in Firefox 3.x, but it does not seem to work in Firefox 4. 它在Firefox 3.x中运行良好,但在Firefox 4中似乎不起作用。

Here's my code: 这是我的代码:

window.addEventListener("load", function() { MyExt.init(); }, false);  

var MyExt = {

  targetHost: "somewebsite.com",

  init: function() {     
     var appcontent = document.getElementById("appcontent");   // browser  
     if (appcontent){  
      appcontent.addEventListener("DOMContentLoaded", MyExt.onPageLoad, true);  
     }
   },

   onPageLoad: function(aEvent) {  
     var doc = aEvent.originalTarget; // doc is document that triggered "onload" event  
     var loc = doc.location;
     var host = '';
     if (loc.toString() != "about:blank") {
       host = doc.location.host;
     }


     // Edit page         
     if (host == MyExt.targetHost) {
        var $ = doc.defaultView.wrappedJSObject.$;

        // this works
        $('p').css('color', 'green');

        // this works in Firefox 3.x, but does not work in Firefox 4
        // instead it shows the following error:
        // "Error: uncaught exception: TypeError: handler is undefined" 
        $('.sometextarea').keyup(function(event) { alert('it should work, but does not'); });

        // even this does not work as expected
        // it should display true, but it displays false
        alert($.isFunction(function(){}));

     }
 }

What am I doing wrong? 我究竟做错了什么?

Yes, you have to use wrappedJSObject due to API changes : 是的,由于API的更改 ,您必须使用wrappedJSObject

Specifying xpcnativewrappers=no in your manifest (that is, XPCNativeWrapper automation) is no longer supported. 不再支持在清单中指定xpcnativewrappers = no(即XPCNativeWrapper自动化)。 This was always intended to be a short-term workaround to allow extensions to continue to work while their authors updated their code to use XPCNativeWrappers. 始终打算将其作为一种短期解决方法,以使扩展在作者更新其代码以使用XPCNativeWrappers时继续起作用。

If your add-on depends upon XBL bindings attached to content objects—for example, the ability to call functions or get and set properties created by the XBL binding—you will need to use the XPCNativeWrapper property wrappedJSObject to access wrapped objects. 如果您的附加组件依赖于附加到内容对象的XBL绑定(例如,调用功能或获取和设置XBL绑定创建的属性的能力),则需要使用XPCNativeWrapper属性wrapdJSObject来访问包装的对象。

If you need to be able to call functions or access properties defined by web content, you'll need to do this as well. 如果您需要能够调用函数或访问由Web内容定义的属性,则也需要这样做。 This may be the case if, for example, you've written an extension that adds a delete button to a web mail service, and the service defines a window.delete() function that you need to call. 例如,如果您编写了将删除按钮添加到Web邮件服务的扩展,并且该服务定义了您需要调用的window.delete()函数,则可能是这种情况。

If, on the other hand, all you're doing with content is accessing DOM methods and properties, you've never needed to be using xpcnativewrappers=no in the first place, and should simply remove it from your manifest. 另一方面,如果您对内容所做的全部工作都是访问DOM方法和属性,那么您根本不需要使用xpcnativewrappers = no,只需将其从清单中删除即可。

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

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