Firefox addon page-mod - if the URL does not match

I want to be able to activate the widget if the URL matches some pattern, but the problem is that I also want to disable the widget when the page’s rule-style does not match the URL. Therefore, if I have several tabs open, and if I switch between them, I must somehow disable the widget, if the active URL of the tab does not match the rule or otherwise activates it. Widget status (on / off) Must be changed when loading pages and going through tabs.

I struggled with this for a while and still have not found a solution.

This is where I am now:

// Activates on matching one of the site domains, but I also want to deactivate // it when it does not match var pageMod = require("page-mod"); pageMod.PageMod({ include: ["*.site1.com","*.site2.com"], onAttach: function() { alert("Widget activated!"); }); }); 

Thanks for the help!

+4
source share
1 answer

If I understand correctly what you are trying to do, then page-mod is the wrong solution - you just want to listen to the active tab. Use the tabs module to do this, listen to ready (new URL loaded) and activate (active tab changed):

 var tabs = require("tabs"); tabs.on("ready", function(tab) { if (tab == tabs.activeTab) updateActiveTab(tab); }); tabs.on("activate", function(tab) { updateActiveTab(tab); }); 

Your updateActiveTab() function will need to check tab.url and activate or deactivate the widget. If you want to use templates for them similar to the ones you specify for page-mod , then you need to use an internal match-pattern module like this

 var {MatchPattern} = require("match-pattern"); var patterns = [ new MatchPattern("*.site1.com"), new MatchPattern("*.site2.com") ]; function updateActiveTab(tab) { var matches = false; for (var i = 0; i < patterns.length; i++) if (patterns[i].test(tab.url)) matches = true; if (matches) activateWidget(); else deactivateWidget(); } 

But of course, you can just use a regular expression or something similar to test tab.url , you do not need to use the match-pattern module.

Disclaimer: The code examples are only there to facilitate understanding of the approach, they have not been tested.

+1
source

Source: https://habr.com/ru/post/1394996/


All Articles