views:

222

answers:

2

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!

+1  A: 
  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.

uthark
I thought of something like that actually. But how will the link signal to the new tab that it should activate the page action?
int3
you can add function `processOutgoingLinkClick()`and render a.href = "javascript:processOutgoingLinkClick('" + element.href + "')"Function `processOutgoingLinkClick` should contain actual processing.
uthark
ah I get it now. However... will I be able to tell whether the user wanted the tab opened in the same page, in a new tab, or in a new window? Still, this would be a minor issue. Thanks!
int3
Hmm, you have target attribute of the original link at least, but I'm not sure if is enough.
uthark
A: 

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.

Mohamed Mansour
well, it doesn't tell me which tab was responsible for creating it...
int3