Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

javascript - 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. 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. 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.

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.

Folder Structure

html

  • index.html

js

  • background.js

css

  • style.css

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Turns out I had to add the right file pathing to access my index.html and update the extension to get it to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...