Safari 扩展,从弹出窗口在新选项卡中打开 url

发布于 12-04 02:34 字数 257 浏览 1 评论 0原文

我的扩展程序从 globalpage.html 打开一个弹出窗口

safari.application.openBrowserWindow()

我想在主窗口的新选项卡中从弹出窗口打开一些网址。我无权访问

safari.extension.globalPage

safari.application.xxxxx

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见2024-12-11 02:34:58

从注入的脚本中,您需要向全局页面发送一条消息,告诉它打开一个新选项卡。

在注入的脚本中:

safari.self.tab.dispatchMessage('openUrlInNewTab', 'http://www.example.com/');

// the message name 'openUrlInNewTab' is arbitrary

在全局页面脚本中:

function handleMessage(msgEvent) {
    if (msgEvent.name == 'openUrlInNewTab') {
        safari.application.activeBrowserWindow.openTab().url = msgEvent.message;
    }
}

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

(这是 Safari 扩展的相关部分开发指南。)

但是,如果您想在最前面的窗口之外的另一个窗口中打开新选项卡(在您的情况下可能是弹出窗口),您需要以某种方式识别另一个窗口。例如,在打开弹出窗口之前,您可以将活动窗口复制到变量中,如下所示:

var targetWin = safari.application.activeBrowserWindow;

然后,当您想在其中打开新选项卡时,请使用:

targetWin.openTab().url = msgEvent.message;

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;
温折酒2024-12-11 02:34:58

这对我有用。
它通过“myCommand”命令链接到工具栏项。
我不需要添加注入脚本,只需将其放在我的 global.html 文件中。

如果选项卡中没有加载 URL,则会禁用该功能,但我通过推荐以“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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文