javascript - Safari Extension Settings to Injected Script -
i'm trying pass variable settings in safari extension injected javascript. i've managed message passed have no idea how use variable out of handlemessage scope can use in injected file. variable printername in handlemessage function appears not accessible outside of function?
global.html
safari.application.addeventlistener('message', handlemessage, false); function handlemessage(msg) { if(msg.name === 'printername') { alert(msg.message); } var printername = safari.extension.settings.printername; safari.application.activebrowserwindow.activetab.page.dispatchmessage('printername', printername); }
injected.js
var printername; function handlemessage(msg) { if(msg.name === 'printername') { printername = msg.message; } } if (window.top === window) { safari.self.addeventlistener('message', handlemessage, false); safari.self.tab.dispatchmessage('printername', printername); alert(printername); }
message passing asynchronous. means injected script sends off message global page, continues next line of code without waiting response (if any). therefore, code depends on response must execute after response received. so, alert(printername)
statement should go inside handlemessage
function.
Comments
Post a Comment