Hi i am making google chrome extension and stucked with error
Uncaught TypeError: Cannot read property 'getSelected' of undefined
My code is below
called function getQueryString(); inside if statement is creating error
key_event.js
if (window == top) {
window.addEventListener('keyup', doKeyPress, false); //add the keyboard handler
}
trigger_key = 37;
function doKeyPress(e){
if (e.ctrlKey && e.keyCode == 37)
{
alert("leftpressed");
getQueryString();
}
else if(e.ctrlKey && e.keyCode == 39){ // if e.shiftKey is not provided then script will run at all instances of typing "G"
alert("rightpressed");
getQueryString();
}
}
function getParameterByName(name,urlPara) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(urlPara);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function getQueryString() {
chrome.tabs.getSelected(null, function(tab) {
var tab = tab.url;
var queryString = {};
var substr = tab.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { queryString[$1] = $3; }
);
var current = getParameterByName('page',tab);
var reExp = 'page=' + current;
current = parseInt(current, 10)-1;
var newUrl = tab.replace(reExp, 'page=' + current);
console.log(newUrl);
chrome.runtime.sendMessage({redirect: newUrl});
});
}
help can bring cheers
Hi i am making google chrome extension and stucked with error
Uncaught TypeError: Cannot read property 'getSelected' of undefined
My code is below
called function getQueryString(); inside if statement is creating error
key_event.js
if (window == top) {
window.addEventListener('keyup', doKeyPress, false); //add the keyboard handler
}
trigger_key = 37;
function doKeyPress(e){
if (e.ctrlKey && e.keyCode == 37)
{
alert("leftpressed");
getQueryString();
}
else if(e.ctrlKey && e.keyCode == 39){ // if e.shiftKey is not provided then script will run at all instances of typing "G"
alert("rightpressed");
getQueryString();
}
}
function getParameterByName(name,urlPara) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(urlPara);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
function getQueryString() {
chrome.tabs.getSelected(null, function(tab) {
var tab = tab.url;
var queryString = {};
var substr = tab.replace(
new RegExp("([^?=&]+)(=([^&]*))?", "g"),
function($0, $1, $2, $3) { queryString[$1] = $3; }
);
var current = getParameterByName('page',tab);
var reExp = 'page=' + current;
current = parseInt(current, 10)-1;
var newUrl = tab.replace(reExp, 'page=' + current);
console.log(newUrl);
chrome.runtime.sendMessage({redirect: newUrl});
});
}
help can bring cheers
Share Improve this question edited Jul 2, 2014 at 10:19 Jot Dhaliwal asked Jul 2, 2014 at 8:14 Jot DhaliwalJot Dhaliwal 1,5004 gold badges27 silver badges47 bronze badges1 Answer
Reset to default 7Your script key_event.js
is a content script. It is running in the context of the website rather than the extension context. As a result it cannot use all of the chrome
API (like chrome.tabs
).
In order to get the current website URL from inside the website context, you can use window.location.href
. You can also get the query string using window.location.search
. So no need to query chrome.tabs
here.
Something in the lines of
var tabURL = window.location.href;
var queryString = window.location.search;