简体   繁体   English

从 Chrome 扩展上下文菜单打开新标签页

[英]Open new tab from Chrome Extension Context Menu

UPDATE: So because of the way my folder is structured, it looks like instead of chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/index.html it should be chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/html/index.html .更新:因此,由于我的文件夹的结构方式,它看起来而不是chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/index.html它应该是chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/html/index.html When I type this in manually into the search bar it works but is still not working when I update my code to this:当我在搜索栏中手动输入它时,它可以工作,但是当我将代码更新为此时仍然无法工作:

chrome.contextMenus.onClicked.addListener(function (result) {
  if (result['menuItemId'] === 'open-sesame') {
    chrome.tabs.create({ 'url': chrome.extension.getURL("/html/index.html") }, function (tab) {
    })
  }
})

I have a chrome extension where users are presented my extension as a new tab.我有一个 chrome 扩展程序,用户可以在其中将我的扩展程序作为新选项卡显示。 A new feature I am implementing is the ability to switch back to the default google search page and now users can access the extension from the browser action context menu.我正在实施的一项新功能是能够切换回默认的谷歌搜索页面,现在用户可以从浏览器操作上下文菜单访问扩展。 I referenced this answer when trying to come up with a solution.在尝试提出解决方案时,我参考了这个答案

I am now stuck at the contextMenu since when I click on it, I come up to the page where it says file could not be accessed.我现在卡在 contextMenu 上,因为当我点击它时,我来到了它说文件无法访问的页面。

background.js

chrome.contextMenus.create({
  title: "Open",
  contexts: ['browser_action'],
  id: 'open-sesame',

})

chrome.contextMenus.onClicked.addListener(function (result) {
  if (result['menuItemId'] === 'open-sesame') {
    chrome.tabs.create({ 'url': chrome.extension.getURL("index.html") }, function (tab) {
    })
  }
})

The url that the new tab opens to is chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/index.html which seems like it would be correct to open my extension but it is still not working for whatever reason.新标签打开到的 url 是chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/index.html似乎打开我的扩展是正确的,但它仍然无法正常工作。

Folder Structure

html html

  • index.html索引.html

js js

  • background.js背景.js

css css

  • style.css样式.css

Turns out I had to add the right file pathing to access my index.html and update the extension to get it to work.结果我必须添加正确的文件路径才能访问我的index.html并更新扩展名以使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM