循环GM_xmlhttpRequest给出"TypeError Null".在变量上 [英] Looping GM_xmlhttpRequest gives "TypeError Null" on a variable

查看:96
本文介绍了循环GM_xmlhttpRequest给出"TypeError Null".在变量上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中有一些链接.我想计算每个链接的响应,并在链接前面插入数字.这是我所拥有的:

I have some links in a page. I want to count the responses of each link and insert the numbers in front of the links. Here is what I have:

var links = document.evaluate('..../td[1]/font//a[@href]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var headings = document.evaluate('.../td[1]',document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
for(var i = 0; i < links.snapshotLength; i++){
  var urls = links.snapshotItem(i).href;
  GM_xmlhttpRequest({
    method: 'GET',
    url: urls,
    onload function (res){
      var dt = document.implementation.createDocumentType("html", 
          "-//W3C//DTD HTML 4.01 Transitional//EN", "http://www.w3.org/TR/html4/loose.dtd");
          doc = document.implementation.createDocument('', '', dt);
          html = doc.createElement('html');
          html.innerHTML = res.responseText;
          doc.appendChild(html);
      var responses = doc.evaluate('.../tr', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      var nResponse = responses.snapshotLength - 1;
      var numResponse = document.createElement('font');
      numResponse.innerHTML = 
       '<b>' + nResponse +
       '</b>' ;
      headings.snapshotItem(i).parentNode.insertBefore(numResponse, headings.snapshotItem(i));
    }
  });
}

我收到错误消息:

TypeError:headings.snapshotItem(...)为空

TypeError: headings.snapshotItem(...) is null

推荐答案

至少存在3个问题:

  1. 尝试不通过闭包将值传递给GM_xmlhttpRequest的onload.
  2. links上循环播放,但尝试为headings编制索引.
  3. onload属性后的冒号.
  1. Trying to pass a value to GM_xmlhttpRequest's onload without a closure.
  2. Looping on links, but trying to index headings.
  3. Missing colon after the onload property.

(1)GM_xmlhttpRequest异步运行.这意味着在onload触发时,变量iheadings要么是未定义的,要么将是它们的最终值,而不是所需的循环值.

(1) GM_xmlhttpRequest operates asynchronously. Which means by the time onload fires that the variables i and headings will either be undefined or will be their ultimate value, not the loop value you want.

要将值传递给onload,请使用闭包. (在下面的代码中,parseURL提供了闭包.)

To pass a value to onload, use a closure. (In the code below, parseURL provides the closure.)

(2)变量ilinks上循环,但是代码试图使用它为headings编制索引!两者的数量几乎是不可能的(即使存在,实践也很差). 标题"是否始终是链接的父级?如果是这样,使用它.

(2) The variable i is looping on links, but the code is trying to use it to index headings! It is very unlikely that there are the same number of each (and poor practice, even if there are). Is the "heading" always a parent of the link? If so use that.

将它们放在一起,使用如下代码:

Putting it all together, use code like this:

var links = document.evaluate (
    '..../td[1]/font//a[@href]', document, null, 
    XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
);
//-- "Headings" are relative to links

for (var J = links.snapshotLength - 1;  J >= 0;  --J) {
    var targUrl = links.snapshotItem (J).href;
    parseURL (targUrl, J);
}

function parseURL (targUrl, J) {
    GM_xmlhttpRequest ( {
        method: 'GET',
        url:    targUrl,
        onload: function (res) {
            var dt = document.implementation.createDocumentType (
                "html", "-//W3C//DTD HTML 4.01 Transitional//EN", 
                "http://www.w3.org/TR/html4/loose.dtd"
            );
            var doc         = document.implementation.createDocument ('', '', dt);
            var html        = doc.createElement ('html');
            html.innerHTML  = res.responseText;
            doc.appendChild (html);

            var responses = doc.evaluate (
                '.../tr', doc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null
            );
            var nResponse = responses.snapshotLength - 1;
            var numResponse = document.createElement ('font');
            numResponse.innerHTML = '<b>' + nResponse + '</b>';

            var heading     = links.snapshotItem (J).parentNode.parentNode;
            heading.parentNode.insertBefore (numResponse, heading);
        }
    } );
}

这篇关于循环GM_xmlhttpRequest给出"TypeError Null".在变量上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆