jQuery技术:Firefox扩展上的标签独立jQuery

我正在开发基于jQuery的Firefox,如本答案中所述。

在实现了答案中提供的示例之后,eveything工作正常,但问题是Firefox Tabs之间的代码以某种方式链接,而example.doc始终引用最后打开的选项卡。

如何在选项卡之间使代码独立?

这是代码的一个excrept:

 (function() { jQuery.noConflict(); $ = function(selector,context) { return new jQuery.fn.init(selector,context||example.doc); }; $.fn = $.prototype = jQuery.fn; example = new function(){}; example.run = function(doc,aEvent) { if (doc.getElementById("plugin-example")) return; this.doc = doc; this.main = main = $('
').appendTo(doc.body).html('Example Loaded!'); this.main.click(function() { //<--- added this function example.main.html(example.doc.location.href); }); main.css({ background:'#FFF',color:'#000',position:'absolute',top:0,left:0,padding:8 }); }; // Bind Plugin var delay = function(aEvent) { var doc = aEvent.originalTarget; setTimeout(function() { example.run(doc,aEvent); }, 1); }; var load = function() { gBrowser.addEventListener("DOMContentLoaded", delay, true); }; window.addEventListener("pageshow", load, false); })();

    您的代码(覆盖脚本)每个窗口只运行一次,而不是每个选项卡运行一次。 因此每个窗口只有一个example实例。 因此example.doc将被设置为最后调度的DOMContentLoaded

    您的函数应正确关闭文档并避免全局状态。 这就是我要写的人(然后我会避免像瘟疫一样的jquery(附件)…)

     // Use strict mode in particular to avoid implicitly var declarations (function() { "use strict"; // Main runner function for each content window. // Similar to SDK page-mod, but without the security boundaries. function run(window, document) { // jquery setup. per https://stackoverflow.com/a/496970/484441 $ = function(selector,context) { return new jq.fn.init(selector,context || document); }; $.fn = $.prototype = jq.fn; if (document.getElementById("my-example-addon-container")) { return; } let main = $('
    '); main.appendTo(document.body).text('Example Loaded!'); main.click(function() { //<--- added this function main.text(document.location.href); }); main.css({ background:'#FFF',color:'#000',position:'absolute',top:0,left:0,padding:8 }); }; const log = Components.utils.reportError.bind(Components.utils); // Do not conflict with other add-ons using jquery. const jq = jQuery.noConflict(true); gBrowser.addEventListener("DOMContentLoaded", function load(evt) { try { // Call run with this == window ;) let doc = evt.target.ownerDocument || evt.target; if (!doc.location.href.startsWith("http")) { // Do not even attempt to interact with non-http(s)? sites. return; } run.call(doc.defaultView, doc.defaultView, doc); } catch (ex) { log(ex); } }, true); })();

    这是一个完整的附加组件作为要点 。 只需放入jquery的副本就可以了。

    PS: 在扩展问题的jquery中重新发布此信息

      以上就是jQuery教程分享Firefox扩展上的标签独立jQuery相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/541232.html

      (0)
      上一篇 2021年1月3日
      下一篇 2021年1月3日

      精彩推荐