am experimenting with the chrome extensions API and I ran into a problem which I don't understand,
I have a background script "background.js" and a content script "content_script.js".
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({code:"console.log('background script')"});
chrome.tabs.executeScript({file:"javascript/content_script.js"});
});
content script
chrome.tabs.executeScript({code:"console.log('content_script')"});
The console.log in the background script works perfectly, but in the one in content_script, I get an error --> "Cannot read property 'executeScript' of undefined"
This means that I am not able to access chrome object, or the chrome.tabs object from the content script. Why is this so ?
content_script.js
chrome.runtime.sendMessage({ from: 'content_script', message: 'Information from webpage.' });
background.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.from == 'content_script') {
console.log('The request has been received from the content script.');
}
});