Addon using pageMod loads content script xpath.js to an open page:
//addon side mod = PageMod({ include: ['*'], contentScriptFile: data.url("./addonside/contentScript.js"), onAttach: function(worker) { worker.on('message', function(message) { console.log('mouseclick: ' + message); }); } }); The script defines global variables that are used to change the attributes of DOM elements:
//content script var oldAttribute; var previousElem = null; var previousElemAttr = null; var allElems = []; // будет использован для подсветки дочерних элементов Event handlers are defined, within which the attributes of the DOM elements change:
//content script window.addEventListener('click', clickListener, true); //"true" kill button click window.addEventListener('mouseover', mouseoverListener, true); window.addEventListener('mouseout', mouseoutListener, true); Inside handler functions, global variables are always available .
From the addon to the content script can be sent a message:
//addon side mod.port.emit("alert", messageFromAddon); But global variables are NOT available for the handler of this message:
//content script self.port.on('alert', function(messageFromAddon){ ..... previousElem.setAttribute('style', previousElemAttr); // восстанавливаем исходные атрибуты кликнутому элементу .... }); When processing an event, the following error occurs:
console.error: JPM [error] Message: TypeError: previousElem is null The biggest oddity is that this is not always the case! Such a sequence of actions eliminates the error:
- running firefox with
jpm run -b /usr/bin/firefox - loading content script with pageMod
- Entering the URL into the browser's address bar and opening the page
In this case, the previousElem no longer be null , and working with it is always normal , I can access the attributes of the DOM element.
I want to understand what is happening and how to fix it?
log:
console.error: JPM [error] Message: TypeError: previousElem is null Stack: @resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/loader/sandbox.js -> resource://firetemplate/data/addonside/xpath.js:5:2 onEvent@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/loader/sandbox.js -> resource://gre/modules/commonjs/sdk/content/content-worker.js:45:22 onEvent@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/loader/sandbox.js -> resource://gre/modules/commonjs/sdk/content/content-worker.js:45:22 onChromeEvent@resource://gre/modules/commonjs/toolkit/loader.js -> resource://gre/modules/commonjs/sdk/loader/sandbox.js -> resource://gre/modules/commonjs/sdk/content/content-worker.js:91:16 EDIT A simple example to play a bug: AddonForSO
var previousElem = null;. The second. Describe the sequence of actions when changing the code in the supplement. Those. how do you run it in the browser normally. And third, in the console of the browserCtrl+Jare there any other errors related to this add-on? - edempreviousElemwas initially initialized as null, but later in the clickListener function, it is assigned a different value:var elem = event.target; previousElem = elem;var elem = event.target; previousElem = elem;And between two different clicks, thepreviousElemvariable contains the old dom element - Alexandermod.port.emit("alert", messageFromAddon);only after the content script was loaded and only after some dom element was selected with a mouse click and thepreviousElemvariable was initialized. Between different clicks (and different launches of handling theclickevent in theclickListenerfunction), the value of thepreviousElemvariable is preserved - Alexanderwindow.addEventListenerandself.port.onwork with different contexts - Alexander