0

I want to load a new page in current tab by clicking on link displayed in the Chrome extension. How can I do that?

I tried assigning the onClick attribute but it does not work for security reasons.

dhruvenvora
  • 157
  • 1
  • 6

2 Answers2

2

You need to find the current tab, and then update it. chrome.tabs is the API to use.

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.update(tabs[0].id, {url: "http://example.com/"});
});

Note that this does not need a "tabs" permission.


And in general, regarding the onClick problems, see this documentation and this question.

Community
  • 1
  • 1
Xan
  • 66,873
  • 13
  • 150
  • 174
0

You can use programmatic injection. Just add a listener for click event which executes the script in the tab. Then this script will change the url.

Stefano Nardo
  • 1,373
  • 1
  • 11
  • 23
  • That's quite a roundabout way to do it. – Xan Feb 04 '16 at 12:54
  • Another issue is that you need permissions to do the injection in the first place. `"activeTab"` will help, but this needs to be addressed in the answer. – Xan Feb 04 '16 at 14:01