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!
views:
222answers:
2
+1
Q:
Chrome browser extensions: How to activate page action for all outgoing links of a certain page?
+1
A:
- Google Chrome API doesn't have such API, but functionality you want may be implemented using standard Google chrome Extensions API.
- You need to implement content script
- 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
2010-03-20 16:49:36
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
2010-03-20 17:15:24
you can add function `processOutgoingLinkClick()`and render a.href = "javascript:processOutgoingLinkClick('" + element.href + "')"Function `processOutgoingLinkClick` should contain actual processing.
uthark
2010-03-20 17:19:48
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
2010-03-20 17:27:48
Hmm, you have target attribute of the original link at least, but I'm not sure if is enough.
uthark
2010-03-20 17:34:06
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
2010-03-20 19:48:41