Toonces
Toonces

Reputation: 11

Passing urls from content to background

I'm quite new when it comes to creating extensions for Chrome and have the worst time trying to grasp concepts.

What I'm hoping to do, eventually, is create an extension which will highlight all links in a page which I currently have bookmarked.

I'm currently just trying to feel my way around, and looking to grab the links and pass them to the background page, but it doesn't seem to be working for me. When I attempt to pass a link I get nothing (or object {} and then a never-ending sea of arrows pointing to various things) If I pass something else, such as "hello", I can get that to work fine. I'd be grateful for some pointers, recommendations et al.

content:

// Get all the links on the page.
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
   console.log(links[i]);
chrome.runtime.sendMessage(links[i]);
}

It does successfully return on links from the content script.

background

var links;
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log(request);              
});

My background script had more in it, but it didn't seem to help in any way so I've pared it back to the most basic of code.

While I would not doubt I'll have questions beyond this, the present issue is preventing me from progressing and I've not been able to figure out what (probably simple thing) I'm doing wrong.

Thanks kindly.

Upvotes: 0

Views: 24

Answers (1)

Toonces
Toonces

Reputation: 11

Thanks! Much appreciated. I'm now moving in the right direction.

For those of you (maybe one of you) wondering, it was solved simply by adding .href to the end of links[i].

eg;

chrome.runtime.sendMessage(links[i].href);

Upvotes: 1

Related Questions