【问题标题】:Open new tab from Chrome Extension Context Menu从 Chrome 扩展上下文菜单打开新标签
【发布时间】:2021-01-09 06:48:24
【问题描述】:

更新: 因此,由于我的文件夹的结构方式,它看起来应该是 chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/html/index.html 而不是 chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/index.html。当我在搜索栏中手动输入它时,它可以工作,但是当我将代码更新为此时仍然无法工作:

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

我有一个 chrome 扩展程序,用户可以在其中将我的扩展程序作为新标签显示。我正在实施的一项新功能是能够切换回默认的谷歌搜索页面,现在用户可以从浏览器操作上下文菜单访问扩展。我在尝试提出解决方案时引用了this answer

我现在卡在 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) {
    })
  }
})

新标签打开的网址是chrome-extension://meajimhgfmioppbkoppphhkbcmapfngh/index.html,打开我的扩展似乎是正确的,但无论出于何种原因它仍然无法正常工作。

Folder Structure

html

  • index.html

js

  • background.js

css

  • style.css

【问题讨论】:

  • 听起来你的扩展没有index.html

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

原来我必须添加正确的文件路径才能访问我的index.html 并更新扩展名才能使其正常工作。

【讨论】:

    猜你喜欢
    • 2011-06-26
    • 2013-10-20
    • 2012-03-23
    • 1970-01-01
    • 2015-05-17
    • 2017-04-11
    • 2019-11-30
    • 1970-01-01
    • 2017-11-14
    相关资源
    最近更新 更多