// ==UserScript== // @name HiFiNi 助手 // @namespace tampermonkey.net // @version 0.4.1 // @description HiFiNi、5song 下载助手。1、自动填写回复内容;2、自动提取蓝奏(修改超链接),自动下载(下载窗口可能会被浏览器拦截); // @tag HiFiNi // @author foobar // @match https://www.hifini.net/thread-*.htm // @match https://www.hifini.net/sg_sign.htm // @match https://hifiki.com/thread-*.htm // @match https://hifiki.com/sg_sign.htm // @match https://www.hifiti.cc/thread-*.htm // @match https://www.hifiti.cc/sg_sign.htm // @match https://www.hifiii.com/thread-*.htm // @match https://www.hifiii.com/sg_sign.htm // @match https://www.5song.xyz/*/*.html // @match https://*.lanzoui.com/* // @match https://*.lanzouo.com/* // @match https://*.lanzoux.com/* // @match https://*.lanzn.com/* // @icon https://www.hifini.net/view/img/logo.png // @grant none // @run-at document-end // @license LGPL // ==/UserScript== (function () { 'use strict'; const CHANNEL = '[HiFiNi]'; let logger = window.console; //region utils function displaying(el) { return window.getComputedStyle(el).display !== 'none'; } function copyText(text) { return navigator.clipboard.writeText(text); } function domesticate(text) { const doc = new DOMParser().parseFromString(text, 'text/html'); return doc.body.firstChild; } function createConsole() { const iframe = document.createElement('iframe'); iframe.name = 'guardian-' + Date.now().toString(36); iframe.style.display = 'none'; document.documentElement.appendChild(iframe); return iframe.contentWindow.console; } //endregion //region HiFiNi /** * @typedef {Object} Adaptor * @property {string} [name] - 名称 * @property {string} postSelector - 主帖选择器 * @property {function(post:HTMLElement): boolean} hasReplied - 是否已回复 * @property {boolean} [suppressConsole] - 是否拦截 console */ /** * * @returns {Partial} */ function customAdaptor() { const host = location.hostname; if (host.indexOf('hifini.com') >= 0) { return {name: 'HiFiNi', postSelector: '.card.jan'} } if (host.indexOf('hifini.net') >= 0) { return { name: 'HiFiNi', /** * @param post {HTMLElement} * @returns {boolean} */ hasReplied(post) { const el = post.querySelector('.alert.alert-warning'); if (el) { logger.info(CHANNEL, '添加回复链接'); el.insertAdjacentHTML('beforeend', ' 回复 '); } return !el; } } } if (host.indexOf('hifiki.com') >= 0) { return {name: 'HiFiKi'}; } if (host.indexOf('hifiti.cc') >= 0) { return {suppressConsole: true}; } return {}; } /** * * @type {Adaptor} */ const adaptor = Object.assign({ name: location.hostname.split('.').at(-2), postSelector: '.card.card-thread', hasReplied() { return !document.querySelector('.alert.alert-warning .post_reply'); } }, customAdaptor()); if (adaptor.suppressConsole) { logger = window._logger = createConsole(); logger.info(`%c${CHANNEL} guard logger`,'color: green; font-weight: bold; padding: 2px 5px; border:solid 1px blue;'); } function shorthandReply(waiting) { let titles = document.title.split('['); const title = titles[0] ?? '卧槽,这么好的歌你怎么不早点告诉我'; document.getElementById('message').value = title; copyText(title).then(); if (waiting) { let btn = document.querySelector('#submit'); btn.textContent = btn.textContent + ' ' + adaptor.name; let timerId; function waitReply() { if (findLanZou()) { clearInterval(timerId); } } if (btn) { btn.addEventListener('click', () => { timerId = setInterval(waitReply, 500); }); } } } function filterText(el) { const spans = el.querySelectorAll('span'); return Array.from(spans) .filter(x => displaying(x)) .map(x => x.innerText) .join(''); } function findBaiDu() { let links = document.querySelectorAll('p a'); let link; for (let i = 0; i < links.length; i++) { if (links[i].href.indexOf('pan.baidu.com') > 0) { link = links[i]; break; } } if (!link) return false; let list = document.querySelector('.alert.alert-success'); const pwd = filterText(list); if (link.href.indexOf('pwd=') < 0) { link.href += `?pwd=${pwd}`; link.textContent += `?pwd=${pwd}`; } return true; } function findLanZou() { let list = document.querySelectorAll('.alert.alert-success'); for (let i = 0; i < list.length; i++) { const el = list[i]; const link = el.querySelector('a'); if (!link) continue; const href = link.href; if (href.indexOf('lanz') > 0 && href.indexOf('.com') > 0) { const pu = filterText(el); link.href = href + '?pu=' + pu; el.prepend('!! '); el.style.color = 'green'; return true; } } return false; } /** * * @returns {Promise} */ function waitHiFiNi() { /** * @param {HTMLElement} post */ function handleLrc(post) { const list = post.querySelectorAll('p:has(> br)'); let found = false; for (let i = 0; i < list.length; i++) { let p = list[i]; if (p.querySelectorAll('br').length > 6) { found = true; p.style.maxHeight = '120px'; p.style.overflowY = 'scroll'; logger.info(CHANNEL, '缩减歌词高度', i); } } if (found) return; const box = post.querySelector('.message.break-all'); if (!box) return; const range = document.createRange(); let start; if (box.querySelectorAll('br').length > 6) { // hifiki.com 大段 br 的歌词 const headings = box.querySelectorAll('h5'); if (headings.length < 2) { logger.warn(CHANNEL, '主帖内未找到足够的 h5 标签'); } else { start = headings[0]; range.setStartAfter(start); range.setEndBefore(headings[1]); } } else { // hifini.net 大段 p 的歌词 const list = box.querySelectorAll('p'); if (list.length > 6) { start = list[0]; range.setStartAfter(start); range.setEndBefore(box.querySelector('h5')); start.insertAdjacentHTML('beforebegin', '
歌词:
'); } } if (start) { const fragment = range.extractContents(); const p = document.createElement('div'); p.appendChild(fragment); start.insertAdjacentElement('afterend', p); p.classList.add('lrc-body'); p.style.maxHeight = '120px'; p.style.overflowY = 'scroll'; logger.info(CHANNEL, '缩减歌词高度', 'after-player'); } } return new Promise((resolve, reject) => { let n = 1; let id = setInterval(() => { const el = document.querySelector(adaptor.postSelector); if (el) { handleLrc(el); clearInterval(id); resolve(el); } if (n++ > 10) { clearInterval(id); reject(`10s内未找到主贴 (${adaptor.postSelector})`); } }, 1e3); }); } async function handleHiFini() { try { const el = await waitHiFiNi(); const replied = adaptor.hasReplied(el); logger.info(CHANNEL, '已回复:' + replied); if (replied) { findLanZou(); findBaiDu(); } else { shorthandReply(true); } } catch (e) { logger.error(CHANNEL, e); } } //endregion //region HiFini sign function handleHiFiniSign() { const text = `
`; const el = domesticate(text); const form = el.children[0]; const input = form.querySelector('input'); form.addEventListener('submit', (e) => { const word = input.value.trim(); if (word) { const kw = encodeURIComponent(word).replaceAll('%', '_') window.location = `/search-${kw}-1.htm`; } e.preventDefault(); }); document.querySelector('.msign').appendChild(el); const sign = document.querySelector('#sign'); if (sign && sign.textContent.indexOf('已签') > -1) { input.focus(); } } //endregion //region LanZou function rushLanZou() { const params = new URLSearchParams(document.location.search); const pickup = params.get('pu'); if (!pickup) return; const pwd = document.getElementById("pwd"); if (!pwd) return; pwd.value = pickup; const btn = document.querySelector('.passwddiv-btn, .btnpwd'); btn?.click(); const timerId = setInterval(autoDown, 500); let times = 0; function autoDown() { times++; const link = document.querySelector("#downajax a") if (link) { link.style.backgroundColor = 'green'; link.innerHTML += ' !'; link.click(); // link.dispatchEvent(new MouseEvent('click', {bubbles: true})); clearInterval(timerId) } else if (times > 10) { clearInterval(timerId); alert("无法下载,未找到下载按钮"); } } } //endregion //region 5song function handle5Song() { let el = document.querySelector('.mobileDown'); if (!el) { return; } el = el.parentElement; el.addEventListener('click', (e) => { const url = e.target.parentElement.dataset.url; if (url) { window.open(url, '_blank') e.preventDefault(); e.stopPropagation(); } else { logger.warn('没找到 url. target:', e.target); alert('没找到 url') } }, {capture: true}); } //endregion const hostname = window.location.hostname; function maybeHiFi() { const words = ['hifini.net', 'hifiki.com', '.hifiii.com', 'hifiti.cc']; for (let word of words) { if (hostname.indexOf(word) > -1) { logger.info(CHANNEL, '检测到 HiFiNi', word) return true; } } return false; } if (maybeHiFi()) { if (document.location.pathname.indexOf("sg_sign.htm") > -1) { handleHiFiniSign(); } else { handleHiFini().then(); } } else if (hostname.indexOf("5song.xyz") > -1) { handle5Song(); } else if (hostname.indexOf("lanz") > -1) { rushLanZou(); } else { logger.warn(' host:', hostname); } })();