Reputation: 1500
chrome-extension, stucked with the error
Uncaught TypeError: Cannot read property 'sendRequest' of undefined
here is my code
manifest.json
{
"manifest_version": 2,
"name": "blah",
"version": "1.0",
"description": "blah",
"browser_action": {
"default_icon": "icon.png"
},
"background": "bg.html", // change to your background page
"permissions": ["http://*/*", "tabs"], //need permission to access all pages & tabs
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"], // run for http & https pages
"js": ["key_event.js"], // key_event.js is injected to the page, this handles key press
"run_at": "document_start" // run before everything else, else there will be conflicts at pages which accept keyboard inputs ( eg:google search)
}
]
}
key_event.js
if (window == top) {
window.addEventListener('keyup', doKeyPress, false); //add the keyboard handler
}
function doKeyPress(e){
if (e.keyCode == 17){ // if e.shiftKey is not provided then script will run at all instances of typing "G"
alert("pressed");
chrome.extension.sendRequest({redirect: "https://www.google.co.in"});//build newurl as per viewtext URL generated earlier.
}
}
bg.html
chrome.extension.onRequest.addListener(function(request, sender) {
chrome.tabs.update(sender.tab.id, {url: request.redirect});
});
plz help me
Upvotes: 0
Views: 147
Reputation: 16574
As already mentioned in comments
The background
section of manifest version 2 has to be like
"background": {"scripts": ["bg.js"]}
There is no background page, only background scripts. So you'll have to move your code from bg.html to bg.js and remove all the extra HTML from it.
chrome.extension.sendRequest
and chrome.extension.onRequest
have been deprecated in favor of chrome.runtime.sendMessage
and chrome.runtime.onMessage
respectively. That means, you can still use sendRequest
and onRequest
but it might be subject to removal in a future version of ChromeG
is 71 rather than 17Upvotes: 2