문제

I'm developing a safari extension. I successfully created a global page which reacts to the toolbar button press. This action opens a new tab in the active window.

My question now is: how can I access defined variables from the global page in my newly opened browser-tab which contains the content html file from the extension? Is it possible to share variables and functions like the popover does?

safari.extension.globalPage.myFunction(); does not work...

Thanks sn3ek

도움이 되었습니까?

해결책

A content script can only communicate with the global page via message passing.

content.js

safari.self.addEventListener('message', handleMessage, false);

// Send a message to the global page
safari.self.tab.dispatchMessage('requestVariables');

// Receive a message back from the global page
function handleMessage(msg) {
    if (msg.name === 'returnVariables') {
        console.log(msg.message);
    }
}

global.js

safari.application.addEventListener('message', handleMessage, false);

// Receive a message from the injected script
function handleMessage(msg) {
    if (msg.name === 'requestVariables') {
        var message = {prop: 'val'};
        safari.application.activeBrowserWindow.activeTab.page.dispatchMessage('returnVariables', message);
    }   
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top