Javascript 从网页特定div/table元素中的链接下载所有pdf
我有一些链接到网页内表格中给出的PDF,我想通过javascript下载它们,我该怎么办 编辑:Javascript 从网页特定div/table元素中的链接下载所有pdf,javascript,html,pdf,download,Javascript,Html,Pdf,Download,我有一些链接到网页内表格中给出的PDF,我想通过javascript下载它们,我该怎么办 编辑: 显然,我这里有一个网页,我只是在浏览器中查看它,我想做一些javascript,它会自动将所有PDF加载到一个特定的表中,仅此而已。有一个简单的javascript解决方案 HTML: 假设您的pdf链接包含在具有href属性的标记中。你可以这样做 var links = document.getElementsByTagName('a'); // Do this selection based o
显然,我这里有一个网页,我只是在浏览器中查看它,我想做一些javascript,它会自动将所有PDF加载到一个特定的表中,仅此而已。有一个简单的javascript解决方案 HTML:
假设您的pdf链接包含在具有
href
属性的
标记中。你可以这样做
var links = document.getElementsByTagName('a');
// Do this selection based on your table
for(var count=0; count<links.length; count++) {
var url = links[count].getAttribute('href');
if(url && url.endsWith('.pdf')) {
links[count].dispatchEvent(new MouseEvent('click'));
}
}
var links=document.getElementsByTagName('a');
//根据您的表格进行此选择
对于(var count=0;count最后,我找到了一种正确执行任务的方法,如果您想进一步了解它,请参阅my。此脚本自动执行我上一个问题所需的所有操作,并强制下载pdf,而无需禁用查看器
代码:
(function(ElementTypeToDownload, DownloadFromWhere) {
// from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery and http://stackoverflow.com/questions/16611497/how-can-i-get-the-name-of-an-html-page-in-javascript
// Anonymous "self-invoking" function
if (!(typeof jQuery === 'function')) {
(function() {
var startingTime = new Date().getTime();
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(script);
// Poll for jQuery to come into existance
var checkReady = function(callback) {
if (window.jQuery) {
callback(jQuery);
} else {
window.setTimeout(function() {
checkReady(callback);
}, 20);
}
};
// Start polling...
checkReady(function($) {
$(function() {
var endingTime = new Date().getTime();
var tookTime = endingTime - startingTime;
});
});
})();
}
// http://facebook.com/anders.tornblad
// anders.tornblad@gmail.com
// from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || fileURL.split("/").pop() || 'unknown';
var evt = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': false
});
save.dispatchEvent(evt);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE < 11
else if (!!window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
// from http://stackoverflow.com/questions/4623982/test-if-jquery-is-loaded-not-using-the-document-ready-event
function preInit() {
// wait until jquery is loeaded
if (!(typeof jQuery === 'function')) {
window.setTimeout(function() {
//console.log(count++);
preInit();
}, 10); // Try again every 10 ms..
return;
}
$(ElementTypeToDownload || prompt("Enter the type of element you are going to download, e.g. 'a' for link, 'img' or 'image' for images (do not add quotes, deafult is 'a') ") || 'a', DownloadFromWhere || prompt("jQuery Selector for the place that you want to download content from, e.g. '#DivName', '.ClassName', 'TagName', body by deafult") || 'body').each(function() {
var tag = (ElementTypeToDownload == 'img' || ElementTypeToDownload == 'image') ? 'src' : 'href';
SaveToDisk($(this).attr(tag));
});
}
preInit();
})();
(函数(ElementTypeToDownload,从何处下载){
//从http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery 及http://stackoverflow.com/questions/16611497/how-can-i-get-the-name-of-an-html-page-in-javascript
//匿名“自调用”函数
if(!(jQuery的类型=='function')){
(功能(){
var startingTime=new Date().getTime();
//加载脚本
var script=document.createElement(“脚本”);
script.src=https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
script.type='text/javascript';
document.getElementsByTagName(“head”)[0].appendChild(脚本);
//轮询jQuery是否存在
var checkReady=函数(回调){
if(window.jQuery){
回调(jQuery);
}否则{
setTimeout(函数(){
checkReady(回调);
}, 20);
}
};
//开始轮询。。。
checkReady(函数($){
$(函数(){
var endingTime=new Date().getTime();
var tookTime=endingTime-startingTime;
});
});
})();
}
// http://facebook.com/anders.tornblad
//安德斯。tornblad@gmail.com
//从http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery
函数SaveToDisk(fileURL,fileName){
//对于非IE
如果(!window.ActiveXObject){
var save=document.createElement('a');
save.href=fileURL;
save.target=''u blank';
save.download=fileName | | fileURL.split(“/”).pop()| |“未知”;
var evt=新建MouseEvent('单击'{
“视图”:窗口,
“泡沫”:没错,
“可取消”:false
});
保存调度事件(evt);
(window.URL | | window.webkitURL).revokeObjectURL(save.href);
}
//对于IE<11
else if(!!window.ActiveXObject&&document.execCommand){
var _window=window.open(fileURL,_blank');
_window.document.close();
_window.document.execCommand('SaveAs',true,fileName | | fileURL)
_window.close();
}
}
//从http://stackoverflow.com/questions/4623982/test-if-jquery-is-loaded-not-using-the-document-ready-event
函数preInit(){
//等待jquery被引导
if(!(jQuery的类型=='function')){
setTimeout(函数(){
//console.log(count++);
preInit();
},10);//每10毫秒重试一次。。
返回;
}
$(ElementTypeToDownload | | |提示符(“输入要下载的元素的类型,例如链接为“a”,图像为“img”或“image”(不要加引号,deafult是“a”))))))| |提示符(“要下载内容的位置的jQuery选择器,例如“DivName”,“ClassName”,“TagName”,body by deafult”)| |“body”)。每个(函数(){
var tag=(ElementTypeToDownload='img'| | ElementTypeToDownload='image')?'src':'href';
SaveToDisk($(this.attr(tag));
});
}
preInit();
})();
无论如何,感谢你们帮助在数据下方创建一个按钮来单击它。单击后,所有pdf文件都将开始下载
请确保在您的浏览器中限制多次下载。
在chrome中,单击“下载所有”按钮后,您必须进行
确保在弹出窗口拦截器中单击“允许”,该拦截器将显示在中
地址栏的右侧
函数clickall(){
var pdfs=document.getElementsByClassName('mydata')[0].getElementsByTagName('a');
对于(var i=0;我能给你网站的url。这样写脚本就容易了。如果你已经解决了,那为什么要问呢?我问是因为我发现你不知道如何得到结果,但我在搜索和组合多个so问题时发现了,所以我自己做了一个。无论如何,非常感谢你的帮助。做得好。谢谢你添加<代码>睡眠
间隔请参阅。
var links = document.getElementsByTagName('a');
// Do this selection based on your table
for(var count=0; count<links.length; count++) {
var url = links[count].getAttribute('href');
if(url && url.endsWith('.pdf')) {
links[count].dispatchEvent(new MouseEvent('click'));
}
}
(function(ElementTypeToDownload, DownloadFromWhere) {
// from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery and http://stackoverflow.com/questions/16611497/how-can-i-get-the-name-of-an-html-page-in-javascript
// Anonymous "self-invoking" function
if (!(typeof jQuery === 'function')) {
(function() {
var startingTime = new Date().getTime();
// Load the script
var script = document.createElement("SCRIPT");
script.src = 'https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js';
script.type = 'text/javascript';
document.getElementsByTagName("head")[0].appendChild(script);
// Poll for jQuery to come into existance
var checkReady = function(callback) {
if (window.jQuery) {
callback(jQuery);
} else {
window.setTimeout(function() {
checkReady(callback);
}, 20);
}
};
// Start polling...
checkReady(function($) {
$(function() {
var endingTime = new Date().getTime();
var tookTime = endingTime - startingTime;
});
});
})();
}
// http://facebook.com/anders.tornblad
// anders.tornblad@gmail.com
// from http://stackoverflow.com/questions/3077242/force-download-a-pdf-link-using-javascript-ajax-jquery
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || fileURL.split("/").pop() || 'unknown';
var evt = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': false
});
save.dispatchEvent(evt);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE < 11
else if (!!window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
// from http://stackoverflow.com/questions/4623982/test-if-jquery-is-loaded-not-using-the-document-ready-event
function preInit() {
// wait until jquery is loeaded
if (!(typeof jQuery === 'function')) {
window.setTimeout(function() {
//console.log(count++);
preInit();
}, 10); // Try again every 10 ms..
return;
}
$(ElementTypeToDownload || prompt("Enter the type of element you are going to download, e.g. 'a' for link, 'img' or 'image' for images (do not add quotes, deafult is 'a') ") || 'a', DownloadFromWhere || prompt("jQuery Selector for the place that you want to download content from, e.g. '#DivName', '.ClassName', 'TagName', body by deafult") || 'body').each(function() {
var tag = (ElementTypeToDownload == 'img' || ElementTypeToDownload == 'image') ? 'src' : 'href';
SaveToDisk($(this).attr(tag));
});
}
preInit();
})();