在Chrome浏览器中,基于XPath,在新标签页中打开当前页面的所有URL
(function() {
/**
* 休眠方法
* deplay: 休眠毫秒数
*/
function sleep(delay) {
var start = new Date().getTime();
while (new Date().getTime() < start + delay);
}
/**
* 根据url下载文件(Chrome有效)
*
* 注意:此处需要注意浏览器 URL 右上角,弹窗会被浏览器拦截。点击允许即可。
*/
function downloadWithUrl(urlStr) {
console.warn('urlStr:' + urlStr);
if (urlStr == null) {
return;
}
var a = document.createElement('a');
a.href = urlStr;
a.setAttribute('target', '_blank');
a.click();
}
var nodes = [];
/**
* 根据 XPath 获取 URL
*/
function getUrlWithXPath() {
var xpathExpression = '//*[@id="app"]/div/div[6]/div/section/ul/li/div/div/div[2]/div[3]/a';
var eva = new XPathEvaluator();
var result = document.evaluate(xpathExpression, document.documentElement, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
if (result != null) {
var node = result.iterateNext();
while (node != null) {
nodes.push(node);
// console.warn(node.href);
// this.downloadWithUrl(node.href);
node = result.iterateNext();
}
}
console.error(nodes.length);
}
getUrlWithXPath();
function downloadFiles() {
console.error(nodes.length);
for (var i = 0; i < nodes.length; i++) {
sleep(500);
downloadWithUrl(nodes[i].href);
}
}
downloadFiles();
})();
网友评论