我正在制作一个基于 chrome 扩展的编辑器,我希望用户在收到如下链接时能够在扩展中打开文件:http://myeditor.com/link。 html?file=xxxx
当访问该网站时,我希望:
- 如果打开网址的人有扩展名,请打开
chrome-extension://blahblah/?file=xxx
- 否则显示该文件的只读版本以及安装扩展程序的链接。
现在要做到这一点,我在 link.html 文件中有一个如下所示的脚本:
if (window.chrome) {
if (chrome.app.getDetails() === null) {
chrome.runtime.sendMessage(chromeId, { message: "isInstalled" }, function (reply) {
if (reply) {
chrome.runtime.sendMessage(chromeId, { gotoUrl: document.location.search });
}
});
}
}
在扩展程序的background.html中:
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request) {
if (request.message) {
if (request.message == "isInstalled") {
sendResponse(true);
}
}
if (request.gotoUrl) {
var url = chrome.extension.getURL(request.gotoUrl);
chrome.tabs.create({url: url}); // how do I open this in the same tab rather than create a new tab?
}
}
return true;
});
问题是代码创建了一个新选项卡,有没有办法可以在与我调用它的页面相同的选项卡中打开扩展页面?
最佳答案
实际上 chrome.tabs.update
而不是 chrome.tabs.create
效果很好...
关于javascript - 在与网站相同的选项卡中以编程方式打开 Chrome 扩展页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47447992/