如何在带有扩展名的Safari中使用Button单击来打开新标签页?

时间:2018-12-19 07:31:46

标签: html macos safari onclick safari-extension

我在使用扩展程序的Safari浏览器中打开新标签页时遇到问题。 使用扩展程序构建器,我添加了bar.html

<html>
<body>
  <form id="myForm">
    <input type="button" value="Button1" onclick="window.open('http://www.google.com')"/>
    <button onclick='window.open("https://www.google.com", "_blank");'>Button2</button>
  </form>
</body>
</html>

Button1 Button2 都不会在新标签页或窗口中打开“ google.com”。如果我将它们放在上面的表单中,然后单击 Button2 ,它将在当前标签页中打开我的bar.html,然后从其中单击 Button1 Button2 ,它将在我当前的标签中打开“ google.com”。

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:0)

您是否尝试过使用target_blank

https://www.w3schools.com/tags/att_a_target.asp

答案 1 :(得分:0)

我在Apple Dev库中发现了这个问题,它指出“ 标准window.open()方法无法用于从扩展栏中打开新的标签页和窗口。相反,扩展栏可以访问SafariApplication, SafariBrowserWindow和SafariBrowserTab类,使您可以打开,关闭,激活和操作窗口和选项卡。”

因此,通过将功能与safari.self.browserWindow.openTab配合使用,我可以在新标签页中打开链接。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Safari Developer Reference</title>
        <button onclick="openInTab('http://www.google.com');">Click me</button>
        <script type="text/javascript">
            function openInTab(source){
                var newTab=safari.self.browserWindow.openTab();
                newTab.url=source;
            }
        </script>
    </head>
    <body style="color:#C02020;background:#C0C0C0;">
        <a href="javascript:openInTab('http://www.google.com');"> Google </a>
    </body>

相关问题