Firefox extension: share state -



Firefox extension: share state -

i have firefox overlay extension tree in sidebar. how can maintain tree state synchronized in several windows? illustration in first window added new item in tree, how update tree in other windows?

if can show minimal code (with utilize code modules, observers, broadcasters or else), please help.

i read similar question, did not help: firefox extension - share mutual state between 2 or more windows

the reply in question reference good, short on explanation. should read references links. have duplicated links here.

one way maintain state info outside of window context utilize javascript code modules (jsm). section sharing objects using code modules talks briefly doing this. 1 time have set jsm share data, simply matter of informing each window alter has been made , should update displayed state. accomplished using event define. of sidebars hear particular event in window. there 1 function in jsm runs through windows signalling them need update.

the code signal like:

components.utils.import("resource://gre/modules/services.jsm"); function foreachopenwindow(todo) { // apply function open browser windows var windows = services.wm.getenumerator("navigator:browser"); while (windows.hasmoreelements()) { todo(windows.getnext().queryinterface(components.interfaces.nsidomwindow)); } } function signalupdateneeded(window){ allow event = window.document.createevent("event"); event.initevent("myextensionname-updateavailable",false,false); window.dispatchevent(event); } function sendupdateavailabletoallwindows(){ foreachopenwindow(signalupdateneeded); }

then in code sidebar:

//this imports jsm, not need .jsm extension, can utilize // whatever extension want. components.utils.import("chrome://myextension/content/modulename.jsm"); window.addeventlistener("myextensionname-updateavailable", updatedatafrommodule, false); //instead may need next (or way // top window). needed depend on context in // sidebar code running. should see below code // access main browser window within sidebar. //window.top.addeventlistener("myextensionname-updateavailable", // updatedatafrommodule, false); function updatedatafrommodule(){ //whatever need here. mylocalvariable = myextensionmodule.datastructure.whatever; }

refactoring first code section above looks in module uses 1 variable cut down namespace clutter. code module like:

var exported_symbols = [ "myextensionmodule" ]; components.utils.import("resource://gre/modules/services.jsm"); var myextensionmodule = { datastructure: { whatever: true, you: 1, want: ["here.", "it", "is", "your", "data."] }; foreachopenwindow: function(todo){ // apply function open browser windows var windows = services.wm.getenumerator("navigator:browser"); while (windows.hasmoreelements()) { todo(windows.getnext() .queryinterface(components.interfaces.nsidomwindow)); } }, signalupdateneeded: function(window){ allow event = window.document.createevent("event"); event.initevent("myextensionname-updateavailable",false,false); window.dispatchevent(event); }, sendupdateavailabletoallwindows: function(){ this.foreachopenwindow(this.signalupdateneeded); } }

i have not tested this, there may errors.

having either sidebar code access main browser window, or jsm code find sidebar code in (in order send or hear fro events) may bit more complicated think. should see working windows in chrome code. specifically, accessing elements of top-level document kid window. section provides next code access main browser window within sidebar:

var mainwindow = window .queryinterface(components.interfaces.nsiinterfacerequestor) .getinterface(components.interfaces.nsiwebnavigation) .queryinterface(components.interfaces.nsidocshelltreeitem) .roottreeitem .queryinterface(components.interfaces.nsiinterfacerequestor) .getinterface(components.interfaces.nsidomwindow);

an alternative jsm maintain reference object in info construction on of sidebars place listeners. object creates. if utilize method , take utilize window, need create sure handle releasing reference if window closed. if don't end memory leak.

firefox firefox-addon state javascript

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -