繁体   English   中英

从链接中获取特定类的文本

[英]Get text from a specific class from a link

使用javascript,我试图打开一个特定的页面,所以我使用window.open()。 我想搜索值并存储它们,这些值与特定类相关联。 我一直在搜索,但我不知道如何做到这一点,我尝试了 window.open(#link).getElementsByClassName 但这不起作用,也没有 link.querySelectorAll。 有没有人有任何提示?

/* JavaScript */

class webpage {
  constructor(url) {
    this.url = url;
    this.frame = document.createElement("iframe");
    this.frame.setAttribute("style", "display: none; position: absolute; left: -99999px; top: -99999px;");
    document.body.appendChild(this.frame);
  }
  load() {
    return new Promise(function(resolve, reject) {
      var http = window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
      http.open("GET", this.url);
      http.addEventListener("load", function() {
        if(http.status == 200 && http.readyState == 4) {
          this.frame.contentWindow.document.write(http.responseText);
          resolve();
        } else if(http.readyState == 4) {
          reject(Error("Something went wrong"));
        }
      }.bind(this));
      http.addEventListener("error", function() {
        reject(Error("Something went wrong"));
      });
      http.send();
    }.bind(this));
  }
  get(query) {
    return (this.frame.contentWindow.document || this.frame.contentDocument).querySelectorAll(query);
  }
}

我创建了一个名为“网页”的类。 例如,您可以像这样构建代码:

/* JavaScript */

var page = new webpage("somepage.net/");// Only works for other domains if the CORS header 'Access-Control-Allow-Origin' is set
page.load().then(function(){
  var list = page.get("div.className"),
      element = list[0];
  alert(element.innerHTML);
});

但是,它仅适用于您自己的域或允许“访问控制允许来源”的域。 有关“ 访问控制允许来源”的更多信息,请点击此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM