javascript - Access current tab page components from script.js mozilla addon-sdk -
i have started learning firefox addon development using addon-sdk. developing addon has button. when click on button panel displays , loads page in panel. code panel below:
var panels = require("sdk/panel"); var panel = panels.panel({ contenturl: self.data.url("mypage.html"), height:380, width:300 });
now want access components(eg. input type text) of current tab page of browser "script.js" file included in "mypage.html". cant post images have 6 reputation. please help.
in index.js
(or main.js
):
panel.port.emit('tag', 'msg');
sends 'msg'
message on panel
's port, tagged 'tag'
.
in script.js
(read further understand file is):
self.port.on('tag', function ( msg ) { alert(msg); });
listens messages on script's dedicated port (here port provided panel
, script.js
attached), tagged 'tag'
.
and backwards: self.port.emit
in script.js
, panel.port.on
in index.js
...
you need take approach in order communicate between main add-on scripts , panel's injected content scripts.
also! in order make work need separate html file (your mypage.html
) javascript
inside of it:
var panel = require('sdk/panel').panel({ contenturl: self.data.url('mypage.html'), // var self = require("sdk/self") contentscriptfile: self.data.url('some-content-script.js') });
so in case:
- access tab within
index.js
, data. - send data panel's
some-content-script.js
, using panel's in-built port (panel.port.emit('tag', 'data')
). - intercept data in
some-content-script.js
(which attachedpanel
) using panel's in-built port (self.port.on('tag', function interceptor (data) {}
). - do want
data
variable (containing'data'
string), insideinterceptor
function.
you can attach multiple .js
content script files panel
this:
var panel = require('sdk/panel').panel({ contenturl: self.data.url('mypage.html'), contentscriptfile: [self.data.url('script_1.js'), self.data.url('script_2.js')] });
on index.js
's side panel
's port panel.port
, whilst on content scripts' side panel
's port known self.port
.
...port.on
(and ...port.once
) intercepting messages port, whilst ...port.emit
posting messages on port.
Comments
Post a Comment