How to create Firefox Extension to send active tab URL to its Native, similar to chrome native messaging and install it through msi -
i have developed c# win form application , chrome extension native messaging (another c# console app) fetch user's active tab url. have developed msi setup in wix write under registry (hklm/software/wow6432node/google/chrome/extensions , hklm\software\wow6432node\google\chrome\nativemessaginghosts) programmatically , tested installation of chrome extension running setup on different windows 7 machines. publishing extension on chrome web store , shall install extension on several client computers.
i want accomplish same mozilla firefox. newbie in field , going through firefox developers guides (xpcom, js-ctypes etc.) , getting little confused multiple links.
it of great if can guide me towards exact solution. please note that, 1) have install extension programmatically through same msi package contains own c# app , chrome extension native app , 2) client machines windows version (xp (optional), 7, 8, server anything).
edit:
according noitidart's answer, have made ffext.xpi 4 files (bootstrap.js, myworker.js, chrome.manifest, install.rdf) , uploaded , installed on firefox, no error, nothing happens. @ first, concentrating on browser extension part , not native , i want have listener inside js files such whenever switch different firefox tab or firefox window, alert should show browser (not native @ moment) displaying active tab's url. don't understand how js files called or executed, how line myworker.addeventlistener('message', handlemessagefromworker);
work or if need add other listener. nothing happening @ all. how debug or proceed here? please help.
to current url of window this:
adomwindow.gbrowser.currenturi.spec
to access windows this:
cu.import('resource://gre/modules/services.jsm'); let domwindows = services.wm.getenumerator(null); while (domwindows.hasmoreelements()) { let adomwindow = domwindows.getnext(); if (adomwindow.gbrowser) { var currenturl = adomwindow.gbrowser.currenturi; } }
if don't check gbrowser
adomwindow.location
chrome path, likely.
so putting togather chromeworker (the chromeworker access js-ctypes) template here: https://github.com/noitidart/chromeworker (this template bare minimum needed communication between chromeworker , js file, in case bootstrap.js) (bootstrap.js js file required , run, 4 startup,shutdown,install,uninstall functions required in bootstrap.js)
from bootstrap.js do, lets in startup:
function startup() { loadandsetupworker(); //must after startup myworker.postmessage({ atopic: 'currenturl', aurl: services.wm.getmostrecentwindow('navigator:browser').gbrowser.currenturi.spec // recent window of type navigator:browser has gbrowser alinkedpanel: services.wm.getmostrecentwindow('navigator:browser').gbrowser.selectedtab.getattribute('linkedpanel') //we use make sure right tab/window on message }); }
then in worker we'll this:
self.onmessage = function(msg) { switch (msg.data.atopic) { case 'currenturl': var ret = msgbox(0, "alert ctypes on windows, url is:" + msg.data.aurl, "ctypes alert windows", mb_ok); self.postmessage({ atopic: 'currenturl-reply', thelinkedpanel: msg.data.alinkedpanel, aresponde: aret }); break; default: throw 'no atopic on incoming message chromeworker'; } }
then in bootstrap.js we'll receive message , it:
function handlemessagefromworker(msg) { console.log('incoming message worker, msg:', msg); switch (msg.data.atopic) { case 'currenturl-reply': let domwindows = services.wm.getenumerator('navigator:browser'); while (domwindows.hasmoreelements()) { let adomwindow = domwindows.getnext(); if (adomwindow.gbrowser && adomwindow.gbrowser.selectedtab.getattribute('linkedpanel') == msg.data.thelinkedpanel) { //this our window, selected tab linkedpanel same 1 worker dealt var currenturl = adomwindow.gbrowser.currenturi; adomwindow.gbrowser.selectedtab.contentwindow.alert('the user prompted js-ctypes , user clicked on:' + aret); break; } } break; default: console.error('no handle msg worker of atopic:', msg.data.atopic); } }
so example does, on install/startup of addon gets recent browser windows url. sends ctypes, ctypes throws os level alert, , ctypes sends user clicked, bootstrap.js finds same window , tab , javascript alert tells jsctypes os level msgbox return value was.
Comments
Post a Comment