Javascript对象返回完整代码而不是结果

时间:2013-04-27 17:03:04

标签: javascript google-chrome google-chrome-extension

我编写了以下Chrome扩展程序以检测用户所在的URL以及处理特定URL中的某些数据。 Background.js包含以下代码:

var triggerURL = "http://xxxxxxxxxxxxxxx/";

function SearchURL(theURL) {
  this.URL = theURL;
  this.checkURLorigin = function() {
    if (this.URL.indexOf(triggerURL) !== -1) {
      console.log("you're on the search page");
      return true;
    }
    else
      return false;
  };
  this.query = function() {
    console.log(this.URL);
    var index = this.URL.indexOf("q=");
    var searchQuery = this.URL.substr(index + 2);
    return searchQuery;
  };
}

chrome.webRequest.onBeforeRequest.addListener(function(details){
  var currentSearchURL = new SearchURL(details.url);
  console.log(currentSearchURL.checkURLorigin);
  if (currentSearchURL.checkURLorigin) {
    msg = currentSearchURL.query;
    console.log("you're on the search page, query = " + msg);
  }
},
{urls: [triggerURL + "*"], types: ["main_frame"]}, ["blocking"]);
然而,

console.log显示SearchURL对象中的代码未执行,而是在控制台中打印了函数的内容。这是为什么?

1 个答案:

答案 0 :(得分:4)

你错过了一个()。正确的电话应该是

 msg = currentSearchURL.query();