最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: Cannot read property 'getSelected' of undefined (Chrome-Extension ) - Stack Ove

programmeradmin1浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

Your 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;

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论