Question

I would like my page action to be activated for all the outgoing links from a certain page. How might I go about doing that? I've gone over the docs to no avail. Any pointers would be appreciated!

Was it helpful?

Solution

  1. Google Chrome API doesn't have such API, but functionality you want may be implemented using standard Google chrome Extensions API.
  2. You need to implement content script
  3. Your content script should modify DOM of the page you want to handle and override all outgoing links with your custom javascript which will do some stuff and open clicked link.

To modify link href you can do something like this:

function processLink(element, newHref) {
  var a = document.createElement("a");
  a.href        = newHref;
  a.textContent = element.textContent;
  a.title       = element.title;

  element.parentNode.replaceChild(a, element);
}

UPDATE 1.

Instead of newHref you can generate something like

a.href = "javascript:processOutgoingLinkClick('" + element.href + "')"

Function processOutgoingLinkClick should contain actual processing of the click.

OTHER TIPS

Just a curiosity, why wont you use the Chrome Extensions Tab Events you can listen for onUpdated onCreated. When a user clicks on a link on the page it will go and fire an event within onUpdated.

So within your background.html, you can do:

chrome.tabs.onUpdated.addListener(function(tabId, info) {
  if (info.status === 'loading')
    console.log('Loading url ... ' + info.url)
});

Same thing for onCreated. Then while its loading, you can decide what to do with your pageAction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top