javascript 即使默认计算机浏览器是“Safari”,如何在 Chrome 浏览器中打开链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26918255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:41:00  来源:igfitidea点击:

How to open a link in chrome browser, even though the default computer browser is 'Safari'?

javascriptgoogle-chrome-app

提问by Nir

I've built a chrome packaged app, and I'm trying to make one of the buttons in it open the chrome browser in a specific link.

我已经构建了一个 chrome 打包应用程序,我试图让其中的一个按钮在特定链接中打开 chrome 浏览器。

For this, I used window.open("http://myLink.com"), which works, but unfortunately it opens the default browser rather than chrome. Is there a way around this?

为此,我使用了window.open("http://myLink.com"),它可以工作,但不幸的是它打开了默认浏览器而不是 chrome。有没有解决的办法?

采纳答案by Xan

This only happens from inside the app window.

这只发生在应用程序窗口内。

If you call window.openfrom the background page, it will open in Chrome.

如果您window.open从后台页面调用,它将在 Chrome 中打开。

So, send it to your background page:

因此,将其发送到您的后台页面:

// app window
function openInChrome(url) {
  chrome.runtime.sendMessage({action: "openURL", url: url});
}

// background
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if(message.action == "openURL") window.open(message.url);
});

回答by sowbug

Use chrome.browser.openTab. See issue. At the moment it's in dev channel.

使用chrome.browser.openTab. 见问题。目前它在开发频道中。