Reputation: 277
My extension opens a popup window from globalpage.html
safari.application.openBrowserWindow()
I would like to open some url from popup window in the new tab of main window. I don't have an access to
safari.extension.globalPage
or
safari.application.xxxxx
Upvotes: 2
Views: 9183
Reputation: 971
This worked for me. It's linked to a toolbar item with the command of "myCommand". I didn't need to add an injection script, and just placed this in my global.html file.
There is a disable if there is no URL loaded in tab, but I turned that off by commending out the line starting in "event.target.disabled..."
<script type="text/javascript" charset="utf-8">
function performCommand(event)
{
if (event.command === "myCommand") {
safari.application.activeBrowserWindow.openTab().url = "http://www.yourdomain.com/";
}
}
function validateCommand(event)
{
if (event.command === "myCommand") {
// Disable the button if there is no URL loaded in the tab.
// event.target.disabled = !event.target.browserWindow.activeTab.url;
}
}
// if event handlers are in the global HTML page,
// register with application:
safari.application.addEventListener("command", performCommand, true);
safari.application.addEventListener("validate", validateCommand, true);
</script>
Upvotes: 1
Reputation: 2829
From your injected script you need to send a message to the global page that tells it to open a new tab.
In the injected script:
safari.self.tab.dispatchMessage('openUrlInNewTab', 'http://www.example.com/');
// the message name 'openUrlInNewTab' is arbitrary
In the global page script:
function handleMessage(msgEvent) {
if (msgEvent.name == 'openUrlInNewTab') {
safari.application.activeBrowserWindow.openTab().url = msgEvent.message;
}
}
safari.application.addEventListener('message', handleMessage, false);
(Here's the relevant section of the Safari extension development guide.)
However, if you want to open the new tab in another window than the frontmost one—which in your case will presumably be your popup—you need to identify the other window somehow. For example, just before you open the popup, you could copy the active window to a variable, like this:
var targetWin = safari.application.activeBrowserWindow;
Then, when you want to open a new tab in it, use:
targetWin.openTab().url = msgEvent.message;
Upvotes: 4