简体   繁体   English

如何从内容脚本链接到扩展程序中的页面?

[英]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 ? 有什么办法可以正确链接它?


Edit: This is the extension. 编辑:这是扩展名。 I'm trying to add a link in the navbar of http://beta.jisho.org/ to <extension>/options/options.html/ . 我正在尝试在http://beta.jisho.org/的导航栏中添加到<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.

相关问题 Chrome 扩展程序:如何获取 web 页面以访问我的内容脚本定义的功能? - Chrome Extension: How do I get a web page to access functions defined by my content script? 如何通过内容脚本将模板中的 HTML 插入 Chrome 扩展程序中的 div - How do I insert the HTML from a template into my div in my Chrome extension via a content script 如何将屏幕截图从扩展程序移动到内容脚本? - How do i move a screenshot from an extension to a content script? 网站的javascript完成页面构建后,如何运行Chrome扩展程序内容脚本? - How do I run Chrome extension content script after a site's javascript has finished building page? 如何编写将内容脚本应用于单页应用程序的 chrome 扩展? - How do I write a chrome extension that applies a content script to a Single Page Application? 如何将 chrome 扩展中的选定文本从后台脚本传递到内容脚本? - how do i pass selected text in chrome extension from background script to content script? Chrome扩展程序-如何将DOM从内容脚本发送到后台页面? - Chrome Extension - How to message DOM from content script to background page? "<i>How can I fetch data in my content script?<\/i>如何在我的内容脚本中获取数据?<\/b> <i>(chrome extension)<\/i> (镀铬扩展)<\/b>" - How can I fetch data in my content script? (chrome extension) 如何从 firefox 扩展的内容脚本发出 GET 请求? - How do I make a GET request from content script of firefox extension? 如何在Firefox Web扩展的内容脚本中使用Wasm? - How do I use Wasm in the content script of a Firefox web extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM