nanuqcz
nanuqcz

Reputation: 1424

Google Chrome extension: Getting all links in page

I am trying to make chrome extension, that can recursively go trough the website and search for common error message strings (like "Error 500" etc.), for web developer testing.

The first step is get all links from the active tab. So, in my extension's background script I have this code snippet:

var $html = jQuery(html);

var links = [];
$html.find('a').each(function(){
    console.log(this);      
    console.log(this.href);
    //links.push(this.href); 
});

But output of this script is weird for relative links:

<a href=​"/​" id=​"logo">​</a>​
chrome-extension://kmldadmcjflefibfflbkpmgjdpklghfa/ 

<a href=​"#top">About</a>​
chrome-extension://kmldadmcjflefibfflbkpmgjdpklghfa/background.html#top 

<a href=​"/articles/applications/​1">​Web applications</a>​
chrome-extension://kmldadmcjflefibfflbkpmgjdpklghfa/articles/applications/1 

Though element's href attribute has the value '/', dumped this.href outputs 'chrome-extension://kmldadmcjflefibfflbkpmgjdpklghfa/'.

How can I get link's href value without 'chrome-extension://blahblahblah/' ?

Upvotes: 1

Views: 6302

Answers (1)

user2226112
user2226112

Reputation:

You are parsing your own extensions background page. Is that really what you want to do?

If your intention is to parse another page, use a "content script". It is extensively documented here:

http://developer.chrome.com/extensions/content_scripts.html

Edit after your comment:

So you just want to get rid of "chrome-extension://..."?

var str = 'chrome-extension://kmldadmcjflefibfflbkpmgjdpklghfa/background.html#top';
var out = str.replace('chrome-extension://kmldadmcjflefibfflbkpmgjdpklghfa', '');

Or better yet, just get both name and URL from your "this".

Upvotes: 2

Related Questions