Last active
December 4, 2024 13:59
-
-
Save lispc/b0e218393eea2dab1e68294c1497b867 to your computer and use it in GitHub Desktop.
博客来电子书自动转化简体中文-Tampermonkey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 博客来简体 | |
// @namespace http://tampermonkey.net/ | |
// @version 0.2 | |
// @description 博客来电子书自动转化简体中文 | |
// @author lispczz | |
// @match https://viewer-ebook.books.com.tw/viewer/epub_v3/* | |
// @require https://cdn.jsdelivr.net/npm/[email protected]/data.min.js | |
// @require https://cdn.jsdelivr.net/npm/[email protected]/data.t2cn.min.js | |
// @require https://cdn.jsdelivr.net/npm/[email protected]/data.cn2t.min.js | |
// @require https://cdn.jsdelivr.net/npm/[email protected]/bundle-browser.min.js | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
console.log('博客来简体'); | |
let globalConverterInstance = null; | |
let converted = false; | |
function convertNode(n, converter) { | |
const oldText = n.innerHTML; | |
const newText = converter(oldText); | |
n.innerHTML = newText; | |
} | |
function getConverter() { | |
if (typeof OpenCC == 'undefined') { | |
return null; | |
} | |
if (globalConverterInstance == null) { | |
globalConverterInstance = OpenCC.Converter({ from: 'tw', to: 'cn' }); | |
} | |
return globalConverterInstance; | |
} | |
function convert() { | |
if (converted) { | |
return; | |
} | |
let converter = getConverter(); | |
if (converter == null) { | |
console.log('博客来简体: converter not ready'); | |
return; | |
} | |
let div = document.querySelectorAll("div.epub-view")[0]; | |
if (div === undefined || div.childNodes[0] === undefined) { | |
return; | |
} | |
let frameDocument = div.childNodes[0].contentDocument; | |
for (let n of frameDocument.querySelectorAll("p, h1, h3, h4")) { | |
convertNode(n, converter); | |
} | |
} | |
setInterval(convert, 500); | |
//const f = EPUBJS.Render.Iframe.prototype.loaded; | |
//EPUBJS.Render.Iframe.prototype.loaded = function(v) { | |
// f.bind(this)(v); | |
// convert(); | |
//} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment