// ==UserScript==
// @name Download Links Helper - 下载链接助手
// @namespace shangandeyu
// @version 1.0.1
// @description 批量复制页面下载链接到剪贴板,支持自定义规则
// @author shangandeyu
// @license GPL-3.0
// @run-at document-end
// @include *
// @require https://code.jquery.com/jquery-3.4.1.min.js
// @grant GM_setClipboard
// @grant unsafeWindow
// @grant GM_listValues
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @grant GM_registerMenuCommand
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
$('head').append(``);
$('body').prepend(`
`);
let modal = document.getElementById("myModal");
let span = document.getElementsByClassName("dlh-close")[0];
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if(event.target == modal) {
modal.style.display = "none";
}
}
let clhBtn = function() {
modal.style.zIndex = [...document.all].reduce((r, e) => Math.max(r, +window.getComputedStyle(e).zIndex || 0), 0) + 1;
modal.style.display = 'block';
}
GM_registerMenuCommand("复制下载链接", clhBtn);
initLinkTab();
})();
unsafeWindow.newRule = function() {
let len = $('div[name="dlh_rule_div"]').length;
if(len > 0) {
$('#rule_div_' + (len - 1)).after(`
保存
删除
`);
} else {
$(`
保存
删除
`).insertBefore($('#dlh_div').children(':first'));
}
}
unsafeWindow.delRule = function(id, i) {
if(id != null) {
GM_deleteValue(id);
}
$('#rule_div_' + i).remove();
unsafeWindow.ruleTab();
initLinkTab();
}
unsafeWindow.saveRule = function(id, i) {
let str = '{"ruleName":"' + $('#rule_name_' + i).val() + '","ruleEx":"' + escape($('#rule_ex_' + i).val()) + '"}';
if(id == null) {
GM_setValue(getUUID(), JSON.parse(str));
} else {
GM_setValue(id, JSON.parse(str));
}
unsafeWindow.ruleTab();
initLinkTab();
}
unsafeWindow.ruleTab = function() {
let emptyRuleHtml = `
保存
删除
`;
let ids = GM_listValues();
let html = '';
if(ids == null || ids.length == 0) {
html = emptyRuleHtml;
} else {
for(let i = 0; i < ids.length; i++) {
html += `
保存
删除
`;
}
}
html += '新建 ';
$('#dlh_div').html(html);
$('#linkTypeSelect').css('z-index', $('#rule_name_0').css('z-index') + 1)
};
unsafeWindow.copyLinks = function() {
let links = '';
$.each($('input[name="dlh_sub"]:checkbox:checked'), function() {
links += $(this).val() + '\n';
});
GM_setClipboard(links, 'text');
alert('已复制到剪贴板,去粘贴吧!');
};
unsafeWindow.showLinks = function(ruleEx) {
ruleEx = eval(ruleEx);
let html = '';
html += `
复制 `;
$('#dlh_div').html(html);
$("#dlh_all").on('click', function() {
$("input[name='dlh_sub']").prop("checked", this.checked);
});
$("input[name='dlh_sub']").on('click', function() {
var $subs = $("input[name='dlh_sub']");
$("#dlh_all").prop("checked", $subs.length == $subs.filter(":checked").length ? true : false);
});
}
function initLinkTab() {
let obj = $('#linkTag');
let html = '';
let ids = GM_listValues();
if(ids == null || ids.length == 0) {
obj.children().first().html('请设置规则 ');
} else {
obj.children().first().html('请选择规则 ');
for(let i = 0; i < ids.length; i++) {
html += '' + GM_getValue(ids[i]).ruleName + ' ';
}
$('#linkTypeSelect').html(html);
// return unescape(GM_getValue(ids[0]).ruleEx);
}
}
function getUUID() {
let s = [];
let hexDigits = '0123456789abcdef';
for(let i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
s[14] = '4'; // bits 12-15 of the time_hi_and_version field to 0010
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
s[8] = s[13] = s[18] = s[23] = '-';
let uuid = s.join('');
return uuid;
}