I'm new to chrome extension development and I want to get the contents of the current tab.
I referred to this question. But I couldn't able to see its working.
This is my background.js
has:
chrome.tabs.onCreated.addListener(function(tab){
console.log("Tab is been created " );
console.log(tab);
console.log(document.body.innerText)
});
chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){
console.log(document.body.innerText)
});
chrome.tabs.onSelectionChanged.addListener(function(tabId,changeInfo,tab){
chrome.tabs.getSelected(null,function(tab){
chrome.tabs.sendRequest(tab.id, {method: "getText"}, function (response) {
console.log("Sending request ")
alltext = response.data;
});
});
});
My content scripts like this:
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getText")
{
console.log("In the request method" + document.all[0].innerText)
sendResponse({data: document.all[0].innerText});
}
else
{
console.log("came here in the else blog")
sendResponse({});
}
});
console.log("Loaded the content ")
The log statement Loaded the content
(but I couldn't able to see the logs coming in the sendRequest
function, Why? ) is getting printed on the console, which means that the content script is loading. And in the extension manifest file I have given all urls like this:
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["contentscript.js"]
}
]
But still I couldn't able to get the data of the current tab. I have been getting the following exception:
Error in event handler for (unknown): TypeError: Cannot read property 'data' of undefined
at chrome-extension://gndneapginhhkepodhngcbcbknfjfobj/background.js:33:21
at disconnectListener (extensions::messaging:338:9)
at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14)
at Event.dispatchToListener (extensions::event_bindings:394:22)
at Event.dispatch_ (extensions::event_bindings:378:27)
at Event.dispatch (extensions::event_bindings:400:17)
at dispatchOnDisconnect (extensions::messaging:293:27)
where I'm making mistake?