简体   繁体   中英

Get text from a specific class from a link

Using javascript, I'm trying to open a specific page, so I'm using window.open(). I want to search values and store them, values that are associated with a specific class. I have been searching a lot but I can't find out how to do that, I tried window.open(#link).getElementsByClassName but that doesn't work, nor link.querySelectorAll. Has anyone got any tips?

/* 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);
  }
}

I have created a class called 'webpage'. For example, you can build your code like this:

/* 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);
});

however, it only works within your own domain or one for which 'Access-Control-Allow-Origin' is allowed. More about ' Access-Control-Allow-Origin ' here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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