// ==UserScript== // @name 轻小说文库美化 // @namespace http://tampermonkey.net/ // @version 2024-02-13 // @description 适配轻小说文库的阅读界面 // @author Le_le // @match https://www.wenku8.net/novel/*/*/* // @icon https://www.wenku8.net/favicon.ico // @license MIT // @grant GM_setValue // @grant GM_getValue // @downloadURL none // ==/UserScript== (function() { 'use strict'; function index_function() { let check_box = document.createElement('input'); check_box.type = 'checkbox'; check_box.id = 'beautify'; check_box.title = 'Beautify'; check_box.addEventListener('change', function () { if (this.checked) { GM_setValue('beautify', true); } else { GM_setValue('beautify', false); } window.location.reload(); } ); if (GM_getValue('beautify', false)) { check_box.checked = true; index_changeherf(); } let label = document.createElement('label'); label.htmlFor = 'beautify'; label.appendChild(document.createTextNode('启用美化')); document.getElementById('linkright').appendChild(label); document.getElementById('linkright').appendChild(check_box); } function index_changeherf() { let tbody = document.getElementsByTagName('tbody')[0]; // 获取所有的tr let trs = tbody.getElementsByTagName('tr'); let a; // 排除第一行 for (let i = 1; i < trs.length; i++) { a = trs[i].getElementsByTagName('a'); for (let i = 0; i < a.length; i++) { if(a[i].innerText.indexOf('插图') > -1) { continue; } a[i].href = a[i].href + '/beautify'; } } } async function beautify_function() { document.body.innerHTML = ""; document.head.innerHTML = ""; let url = window.location.href; // 去除最后的/beautify url = url.substring(0, url.lastIndexOf('/')); await fetchAndDecode(url, 'gbk'); addhtml(); } async function fetchAndDecode(file, encoding) { var xhr = new XMLHttpRequest(); xhr.open('GET', file); xhr.responseType = 'arraybuffer'; xhr.onload = function () { if (this.status == 200) { var dataView = new DataView(this.response); var decoder = new TextDecoder(encoding); var decodedString = decoder.decode(dataView); // 解析html let parser = new DOMParser(); let doc = parser.parseFromString(decodedString, 'text/html'); let content = doc.getElementById('content'); window.contentvalue = content.innerHTML; let title = doc.title; document.title = title; document.getElementById('content').innerHTML = content.innerHTML; } else { console.error('Error while requesting', file, this); } }; xhr.send(); } let locate = window.location; if (locate.href.indexOf('index') > -1) { index_function(); } else if (locate.href.indexOf('beautify') > -1) { beautify_function(); } function addhtml() { let div = `