Skip to content

Instantly share code, notes, and snippets.

@mnixry
Last active October 30, 2021 16:49
Show Gist options
  • Save mnixry/c637d2966eef50fd3bf01a17eef52a0f to your computer and use it in GitHub Desktop.
Save mnixry/c637d2966eef50fd3bf01a17eef52a0f to your computer and use it in GitHub Desktop.
Chinese Marxists.org PDF books batch download script. (with Tampermonkey)
// ==UserScript==
// @name Marxist downloader
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Downlaod PDF books from Marxists.org Chinese
// @author Mix <mnixry@users.noreply.github.com>
// @match https://www.marxists.org/chinese/pdf/*
// @grant GM_download
// @grant unsafeWindow
// ==/UserScript==
setInterval(() => {
const tables = document.querySelectorAll("td");
for (const table of tables) {
if (table.id.trim() !== "") continue;
table.id = randomString(10);
table.innerHTML += /*html*/ `
<button onclick="downloadPdf('${table.id}')">下载此部分</button>
`;
}
}, 2000);
/**
* @param {number} length
*/
function randomString(length) {
const chars =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const result = new Array(length)
.fill(undefined)
.map(() => chars[Math.floor(Math.random() * chars.length)]);
return result.join("");
}
/**
* @param {string} id
*/
function downloadPdf(id) {
const table = document.getElementById(id);
for (const link of table.querySelectorAll("a")) {
const { href: path } = link;
if (typeof path !== "string" || !path.endsWith(".pdf")) continue;
const downloadUrl = new URL(path, location.href);
const filename = link.innerText.trim() + ".pdf";
GM_download({
url: downloadUrl.href,
name: filename,
onload: () => (link.innerText += "(已下载)"),
});
}
}
unsafeWindow.downloadPdf = downloadPdf;

中文马克思主义文库(PDF)浏览器批量下载脚本

基于油猴(Tampermonkey)插件

演示

使用方法

下载安装插件

这个网上教程很多, 不多说

调整下载设定

  1. 点击 油猴图标 ->管理面板->设置
  1. 通用->配置模式 选择高级
  1. 下载BETA -> 下载模式选择浏览器API, 在下方文件扩展名白名单最后一行添加.pdf扩展名
  • 点击保存, 可能会弹出需要更多权限的对话框, 允许即可

安装脚本

  • 选择添加新脚本

  • 用这一页上方的.js文件覆盖其中内容

    • Ctrl+S保存, 或者在编辑器中 文件->保存

理论上没出问题的话, 就装好了, 可以去中文马克思主义文库看看是不是这样

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment