2

I am working on a chrome extension that function nearly the same as open in new tab, but "open in new tab" apparently does not allow open links for intranet files such as file:// due to chrome security reasons. Therefore, by having this extension, the users who has this custom extension will be able to open the url link.

I managed to put something where when i select any part of the link text, then i right click to contextmenu, it is able to open the link in new tab. What i want now is to be able to right click the link without selecting the text.

I have tried changing the contexttype to link, but it doesn't seem to work.

Here is the background.js code that does the selection then open in new tab.

I hope maybe someone can shed some ideas and maybe snippet code as to what i can do here to keep me rolling.

background.js

  var context = "selection";
  var title = "Open Local File";
  var id = chrome.contextMenus.create({
      "title": title,
      "contexts":[context],
      "id": "context" + context}
      );  

// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);

function onClickHandler(info, tab) {
  var sText = info.linkUrl;
  var myUrl = sText;  
  chrome.tabs.create({ url: myUrl });

};

manifest.json

{
  "name": "Right-click context sample",
  "description": "sample",
  "version": "0.0.1",
  "permissions": ["contextMenus", "tabs", "<all_urls>"],
  "background": {
    "page" : "background.html",
    "persistent": false
    //"scripts": ["background.js"]
  },
  "manifest_version": 2
}

Thanks!

Community
  • 1
  • 1
AmazingTrans
  • 21
  • 1
  • 3

2 Answers2

2

Use ContextType.

  var context = "selection";
  var title = "Open Local File";
  var id = chrome.contextMenus.create({
      "title": title,
      "contexts":[context],
      "id": "context" + context,
      contexts:["all"]}
      );  

// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);

function onClickHandler(info, tab) {
  var sText = info.linkUrl;
  var myUrl = sText;  
  chrome.tabs.create({ url: myUrl });

};
Sungguk Lim
  • 6,109
  • 7
  • 43
  • 63
  • Tried your suggestion, this time when i right click a link, there is no contextmenu shown, neither does selecting the link, shows the contextmenu at all. – AmazingTrans Dec 24 '15 at 05:56
0

I have found a solution by setting the following context to all.

  var context = "all";
  var title = "Open Local File";
  var id = chrome.contextMenus.create({
      "title": title,
      "contexts":[context],
      "id": context
  });  
AmazingTrans
  • 21
  • 1
  • 3