Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将事件侦听器添加到';内存对象';用Javascript?_Javascript_Listener_Dom Events - Fatal编程技术网

将事件侦听器添加到';内存对象';用Javascript?

将事件侦听器添加到';内存对象';用Javascript?,javascript,listener,dom-events,Javascript,Listener,Dom Events,这个问题的一些背景: 我正在写一篇文章,它将“发挥”谷歌在线搜索页面预览的功能。本质上,我想在keyup事件中添加一个事件侦听器,它将 查询页面预览结果(向google发送ajax请求) 在内存中读取该结果 实时将其添加到搜索页面 这将实质上创建一个实时页面预览搜索结果,这将是谷歌的搜索结果 公平地说,我实际上已经在各种组件中使用了大约95%的功能,我正在调整功能,使其更加用户友好。如果你对我所做的事情的原因和方式感兴趣,请随时查看我以前的博客帖子,甚至查看“最新版本的代码”(目前非常有问题,

这个问题的一些背景:

我正在写一篇文章,它将“发挥”谷歌在线搜索页面预览的功能。本质上,我想在keyup事件中添加一个事件侦听器,它将

  • 查询页面预览结果(向google发送ajax请求)
  • 在内存中读取该结果
  • 实时将其添加到搜索页面
  • 这将实质上创建一个实时页面预览搜索结果,这将是谷歌的搜索结果

    公平地说,我实际上已经在各种组件中使用了大约95%的功能,我正在调整功能,使其更加用户友好。如果你对我所做的事情的原因和方式感兴趣,请随时查看我以前的博客帖子,甚至查看“最新版本的代码”(目前非常有问题,因为我现在有点“功能之间的差异”)

    浏览器的bookmarklet代码如下:

     javascript:(function{var head= document.getElementsByTagName('head')[0];var script= document.createElement('script');script.type= 'text/javascript';script.src= 'http://chesser.ca/gvs.marklet.0.3.js';head.appendChild(script);};)();
    
    (keyup事件侦听器当前设置为off,基本上是为了使其在当前状态下工作,您可以在具有live preview的页面上运行搜索并单击bookmarklet一次(以启动查询),等待一两秒钟,然后再次单击它(以显示页面预览)

    因此,我要对此进行一些更改:

    在第1部分(性能方面)中,我只对查询前两个元素感兴趣。我想这很容易,唯一让我感到困惑的是我还没有尝试过它(而不是所有divs.length,只需将它设置为2)

    在这里(使用事件侦听器)要做的明显更改是将
    for(google.vs.ha中的var Obj){
    修复为传入的单个
    google.vs.rs
    对象

    如果你一直坚持到这个问题:谢谢阅读:) -亚历克斯

    编辑

    根据下面的讨论,google.vs.Ga似乎负责查询数据(答案是过度编写函数)

    为了提供信息(和乐趣),这里是.Ga代码

      google.vs.Ga = function (a, b, c) {
            var d = google.vs.b.kfe.kfeHost,
                g = google.vs.Ya(a),
                i = a.getAttribute("sig");
            if (i) {
                var f = google.vs.qa(a);
                if (f) {
                    d = [d ? "http://" + d : "", google.vs.b.kfe.kfeUrlPrefix, "&d=", encodeURIComponent(f), "&b=1", "&jsonp=google.vs.r"];
                    d.push("&a=");
                    d.push(encodeURIComponent(i));
                    if (i = a.getAttribute("blobref")) {
                        d.push("&bl=");
                        d.push(i)
                    }
                    d.push("&rs=");
                    i = 0;
                    for (var j; j = g[i++];) {
                        d.push(encodeURIComponent(j));
                        i < g.length && d.push("&rs=")
                    }
                    g = google.vs.m(a) || {
                        ub: a
                    };
                    g.G = c;
                    google.vs.ha[f] = g;
                    c = d.join("");
                    c = new google.vs.Ia(f, c, function () {
                        google.vs.P(a, h);
                        o(google.vs.k, f)
                    });
                    b ? p(google.vs.k, c) : q(google.vs.k, c)
                }
            }
        };
    
    google.vs.Ga=函数(a、b、c){
    var d=google.vs.b.kfe.kfeHost,
    g=谷歌vs.Ya(a),
    i=a.getAttribute(“sig”);
    如果(i){
    var f=谷歌vs.qa(a);
    如果(f){
    d=[d?”http://“+d:”,google.vs.b.kfe.kfeUrlPrefix,“&d=”,encodeURIComponent(f),“&b=1”,“&jsonp=google.vs.r”];
    d、 按(&a=“);
    d、 推(部件(i));
    if(i=a.getAttribute(“blobref”)){
    d、 推送(&bl=);
    d、 推送(一)
    }
    d、 推送(“&rs=”);
    i=0;
    对于(var j;j=g[i++];){
    d、 推动(部件(j));
    i
    google.vs.ha对象是一个基本的JavaScript对象,具有键/值对属性,没有任何函数可供使用。也就是说,这些简单对象在更改时无法通知您

    在我看来,你基本上有两个选择:

    • 定期检查google.vs.ha以查找您正在查找的数据,跟踪哪些数据是您已经抓取的图像

    • 确定页面上或google.namespace中负责加载数据的函数。一旦确定加载数据的位置,以及google.vs.ha对象更新的确切位置,就可以使用包含事件通知的自己制作的函数替换原始函数n

    例如,如果我有一个如下所示的基本函数:

    var Example = function(value){
      var closured = ' world';
      this.value = value;
      this.doSomething = function(){ alert(this.value + closured); };
    };
    
    var test = new Example('hello');
    test.doSomething(); // will only alert 'hello world';
    
    var oldFunc = test.doSomething;
    var notifyMe = function(){ alert('notified'); }; // callback function
    
    // Update previous method to do it's normal thing, but then notify after
    test.doSomething = function(){
      oldFunc.apply(this, arguments);
      notifyMe();
    };
    
    test.doSomething(); // will alert 'hello world', and then 'notified'
    
    在上面的代码中,我们已经用我们自己的函数有效地替换了旧的doSomething函数。这个新版本仍然执行它以前的职责(通过oldFunc.apply),但随后会通知您

    请注意,新函数只能访问对象的“public”属性,而不能访问闭包捕获的私有元素(例如“closured”var).我似乎记得,谷歌倾向于在闭包接近时避免使用私有变量,因为这会增加内存管理的复杂性,所以这可能不是一个问题

    更新:我在谷歌搜索结果页面上玩了一下。我运行了一个新的搜索,但在点击预览之前,我在Chrome控制台中执行了以下操作:

    var old = google.vs.Ga;
    var newFunc = function(){
      old.apply(this, arguments);
      console.log(arguments);
    };
    
    google.vs.Ga = newFunc; 
    

    它似乎是在单击预览后触发的。

    google.vs.ha对象是一个基本的JavaScript对象,具有键/值对属性,没有任何函数可供使用。也就是说,这些简单对象在更改时无法通知您

    在我看来,你基本上有两个选择:

    • 定期检查google.vs.ha以查找您正在查找的数据,跟踪哪些数据是您已经抓取的图像

    • 确定页面上或google.namespace中负责加载数据的函数。一旦确定加载数据的位置,以及google.vs.ha对象更新的确切位置,就可以使用包含事件通知的自己制作的函数替换原始函数n

    例如,如果我有一个如下所示的基本函数:

    var Example = function(value){
      var closured = ' world';
      this.value = value;
      this.doSomething = function(){ alert(this.value + closured); };
    };
    
    var test = new Example('hello');
    test.doSomething(); // will only alert 'hello world';
    
    var oldFunc = test.doSomething;
    var notifyMe = function(){ alert('notified'); }; // callback function
    
    // Update previous method to do it's normal thing, but then notify after
    test.doSomething = function(){
      oldFunc.apply(this, arguments);
      notifyMe();
    };
    
    test.doSomething(); // will alert 'hello world', and then 'notified'
    
    在上面的代码中,我们
    var old = google.vs.Ga;
    var newFunc = function(){
      old.apply(this, arguments);
      console.log(arguments);
    };
    
    google.vs.Ga = newFunc;