[英]How do I link to a page in my extension from a content script?
I'm writing a content script for a site and I'm trying to add a link a page in my extension in the site navbar. 我正在为网站编写内容脚本,并尝试在网站导航栏中的扩展程序中添加页面链接。 I tried this
我试过了
var linksListEl = document.querySelector("#top_bar .links");
var newLink = document.createElement("li");
newLink.innerHTML = '<a href="' + chrome.extension.getURL("options/options.html") + '">Kioku</a>';
linksListEl.insertBefore(newLink, linksListEl.children[0]);
It shows correctly with the right URL, but when I click on it it goes to the blank page with the URL about:blank
. 它使用正确的URL正确显示,但是当我单击它时,它进入带有URL
about:blank
的空白页面。 If I copy the URL of the link and paste in the omnibar I get the page I want, so I think there is some sort of mechanism to prevent webpages to linking to chrome-extension? 如果我复制链接的URL并粘贴到多功能栏中,则会得到所需的页面,所以我认为有某种机制可以防止网页链接到chrome-extension?
Is there any way to link it correctly ? 有什么办法可以正确链接它?
<extension>/options/options.html/
.
<extension>/options/options.html/
。
The content script for that site is jisho-kioku/beta-jisho.js
.
jisho-kioku/beta-jisho.js
。
https://github.com/odraencoded/jisho-kioku https://github.com/odraencoded/jisho-kioku
PS: I don't think the information above is of much value, though :/ PS:我不认为上面的信息有价值,尽管:/
You can take a step sideways and instead have your event page open the options page. 您可以采取一些措施,而是让事件页面打开选项页面。
Content script: 内容脚本:
var linksListEl = document.querySelector("#top_bar .links");
var newLink = document.createElement("li");
newLink.innerHTML = '<a href="#" id="optionsLink">Kioku</a>';
linksListEl.insertBefore(newLink, linksListEl.children[0]);
document.querySelector("#optionsLink").addEventListener("click", showOptions);
function showOptions(){
chrome.runtime.sendMessage({type : "show-options"});
}
Event page: 活动页面:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == "show-options") {
chrome.tabs.update( // Or open a new tab if desired
sender.tab.id,
{url: chrome.runtime.getURL("options/options.html")}
);
}
/* ... */
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.