使用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);
});
但是,仅在您自己的域或允许"Access-Control-Allow-Origin"的域内工作。关于"Access-Control-Allow-Origin"的更多信息请点击这里。