I want to get all titles from reddit, and pass it to backgroundScript.js.
It shows all URLs in the console of the page but for some reason it shows me this message on background page:
My code:
manifest.json:
{
"manifest_version": 2,
"name": "Testing stuff",
"description": "This extension is for testing purposes",
"version": "1.0",
"background": {
"scripts": [ "backgroundScript.js" ]
},
"content_scripts": [ {
"exclude_matches": [ "*://*.reddit.com/r/*/ comments /*" ],
"js": [ "contentScript.js" ],
"matches": [ "*://*.reddit.com/r/*", "*://*.reddit.com/", "*://*.reddit.com/me/m/*", "*://*.reddit.com/user/*/ m /*" ],
"run_at": "document_end"
} ],
"permissions": [ "history", "tabs", "http://*/ *" ]
}
contentScript.js:
titleUrls = document.querySelectorAll("a.title");
for (i = 0; i < titleUrlsArray.length; i++) {
var u = i + 1
console.log(u + " " + titleUrls[i].href);
}
chrome.runtime.sendMessage(titleUrls)
backgroundScript.js:
chrome.runtime.onMessage.addListener(
function (response, sender, sendResponse) {
var data = response;
alert("in backgroundScript.js, received " + data[1].href);
}
);
Why does it show undefined
on the background page when it shows all URLs just fine in the console of the main page? What am I doing wrong?