I'm trying to write an extension for FF. Expansion should change some element on the site.

chrome.manifest

content vkstats chrome/content/ content vkstats chrome/content/ contentaccessible=yes overlay chrome://browser/content/browser.xul chrome://vkstats/content/browser.xul 

content / browser.xul

 <?xml version="1.0"?> <overlay id="vkstats" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> <script type="application/x-javascript" src="chrome://vkstats/content/main.js" /> </overlay> 

content / main.js

 var VKStats = { init: function () { ///////////////////////// var l_ap = document.getElementById('l_ap'); var l_stats = document.createElement('li'); l_stats.innerHTML = 'Тест'; insertAfter(l_stats, l_ap); }, getUserId: function () { var http = new XMLHttpRequest(); http.open("GET", "http://vk.com/", false); http.send(); var user_id = /"user_id":(\d+)/m; return user_id.exec(http.responseText)[1]; } }; function insertAfter(elem, refElem) { return refElem.parentNode.insertBefore(elem, refElem.nextSibling); } 

It seems like the extension is enabled only when the browser is started, but the element is not added. If you add a code to JS, for example alert (111) ;, will work, only once at the start of the browser, regardless of which site the tab is on. Even if you refresh the page, alert will not work anyway.

Help me figure out what the problem is?

 var VKStats = { init: function(){ if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false); }, onPageLoad: function(aEvent){ var doc = aEvent.originalTarget; // doc that triggered the event var win = doc.defaultView; // win is the window for the doc ///////////////////////// var l_ap = doc.getElementById('l_ap'); var l_stats = doc.createElement('li'); l_stats.innerHTML = 'тут текст'; //l_ap.appendChild(l_stats); insertAfter(l_stats, l_ap); }, getUserId: function(){ var http = new XMLHttpRequest(); http.open("GET","http://vk.com/",false); http.send(); var user_id = /"user_id":(\d+)/m; return user_id.exec(http.responseText)[1]; } }; function insertAfter(elem, refElem) { return refElem.parentNode.insertBefore(elem, refElem.nextSibling); } window.addEventListener("load", function load(event){ window.removeEventListener("load", load, false); VKstats.init(); },false); 
  • @or_die: (comments run out there) check the regular expression, surely the problem is there. For example, (..)? looks suspicious. Insert a temporary alert(doc.location.href) before the if 'om. - VladD
  • that does not display anything. - or_die
  • @or_die: earned? - VladD

1 answer 1

It's simple: your initializer and the truth is executed only once, this is the case for all extensions. You need to subscribe to the page loading event (for example, DOMContentLoaded ) and do what you need in it.

Code :

 var myExtension = { init: function() { if(gBrowser) gBrowser.addEventListener("DOMContentLoaded", this.onPageLoad, false); }, onPageLoad: function(aEvent) { var doc = aEvent.originalTarget; // document that triggered the event var win = doc.defaultView; // win is the window for the doc // тут делайте что вам надо } } window.addEventListener("load", function load(event){ window.removeEventListener("load", load, false); // нужно только 1 раз myExtension.init(); },false); 
  • and then, treat as doc.getElementById ('l_ap'); ? - or_die
  • @or_die: then doc is your document. doc.getElementById('l_ap') , but is there such an id in it, a question of chance. The code will be called for all pages. - VladD
  • doesn't work, even alert is not called - or_die
  • @or_die: write your modified code: telepathy is not my fad yet. At leisure, think about what getElementById will return if there is no element on the page, and what it means for your code. - VladD
  • one
    @or_die: (1) check on which page the script worked. Do not try to process all pages: the extension should not work on google.com? (2) check the result of getElementById and exit the script if it is not as it should be. (3) Are you 100% sure that there parentNode always be parentNode and nextSibling ? - VladD