I'm trying to pass a variable from the settings in my safari extension to the injected javascript. I've managed to get the message passed but have no idea how to use the variable out of the handleMessage scope so I can use it in my injected file. The variable printerNAME in the handleMessage function appears not to be accessible outside of the 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);
}