// ==UserScript==
// @name 戴森球蓝图 增强
// @namespace https://blog.rhilip.info/
// @version 0.0.1
// @description Extend the website Dyson Sphere Blueprints
// @author Rhilip
// @match https://www.dysonsphereblueprints.com/*
// @require https://unpkg.com/jquery@3.3.1/dist/jquery.min.js
// @require https://unpkg.com/jszip@3.7.0/dist/jszip.min.js
// @require https://unpkg.com/file-saver@2.0.5/dist/FileSaver.min.js
// @grant none
// @downloadURL none
// ==/UserScript==
(async function () {
'use strict';
function getBlueprintNameFromAnother(another) {
return another.find('div.o-blueprint-card__content h2 a').text().trim();
}
function getBlueprintTextFromAnother(another) {
return another.find('button[data-clipboard-text]').data('clipboard-text');
}
function getBlueprintDataFromAnother(another) {
return [getBlueprintNameFromAnother(another), getBlueprintTextFromAnother(another)]
}
// 给列表的单个蓝图加上下载按钮
function addBlueprintDownloadBtn() {
$('.o-blueprint-card__copy').each(function () {
const that = $(this);
const blueprintString = that.data('clipboard-text');
const blueprintAnother = that.parents('li.o-blueprint-card');
const blueprintName = getBlueprintNameFromAnother(blueprintAnother);
const downloadBtn = $('');
downloadBtn.click(function () {
saveAs(new Blob([blueprintString]), `${blueprintName}.txt`);
})
if (blueprintAnother.find('button.uj_download_this').length === 0) {
that.after(downloadBtn);
}
})
}
function addDownloadThisPageBtn() {
const downloadThisPageBtn = $('');
downloadThisPageBtn.click(function () {
const allBlueprintInThisPage = $('li.o-blueprint-card').map(function () {
return [getBlueprintDataFromAnother($(this))];
}).get();
if (allBlueprintInThisPage.length > 1) {
const jszip = new JSZip();
for (const getBlueprintDataFromAnother of allBlueprintInThisPage) {
const [bpName, bpText] = getBlueprintDataFromAnother
jszip.file(`${blueprintName}.txt`, bpText)
}
jszip.generateAsync({ type: "blob" })
.then(function (content) {
saveAs(content, "blueprint.zip");
});
} else {
const [bpName, bpText] = allBlueprintInThisPage[0];
saveAs(new Blob([bpText]), `${blueprintName}.txt`);
}
})
if ($('button.uj_download_page').length === 0) {
$('div.t-blueprint-list').prepend(downloadThisPageBtn);
}
}
function main() {
$('.uj_added').remove(); // 清空所有我们添加的DOM,防止 turbolinks 切换时出现重复按钮
if (
location.pathname === '/' // 首页
|| (location.pathname === '/blueprints' && location.search.includes('search=')) // 搜索页
) {
console.log('首页以及蓝图搜索页');
addBlueprintDownloadBtn();
addDownloadThisPageBtn();
} else if (
location.pathname === '/collections'
) {
console.log('合集搜索页面');
// TODO 直接在合集搜索页面搜索下载整个合集
} else if (
location.pathname.startsWith('/blueprints/')
) {
console.log('蓝图详细页面');
const downloadBtn = $('');
downloadBtn.click(function () {
const bpText = $('div.t-blueprint__blueprint > textarea').text();
const bpName = $('div.t-blueprint__title h2').text().trim();
saveAs(new Blob([bpText]), `${sanitizeFileName(bpName)}.txt`);
});
$('.t-blueprint__blueprint button.t-blueprint__copy').after(downloadBtn);
} else if (
location.pathname.startsWith('/collections/')
|| /^\/users(\/\d+)?\/blueprints/.test(location.pathname) // 用户列表页(包括自己和他人)
) {
console.log('合集详细页面');
addBlueprintDownloadBtn();
addDownloadThisPageBtn();
}
}
// 运行主方法,并在页面切换时同样运行
main();
document.addEventListener("turbolinks:load", function () {
main();
})
})();