Chrome browser extensions: How to activate page action for all outgoing links of a certain page?
I would like my page action to be activated for all the o开发者_如何转开发utgoing 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!
- 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.
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.
精彩评论