Skip to content

Instantly share code, notes, and snippets.

@lispc
Last active December 27, 2023 16:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lispc/b0e218393eea2dab1e68294c1497b867 to your computer and use it in GitHub Desktop.
Save lispc/b0e218393eea2dab1e68294c1497b867 to your computer and use it in GitHub Desktop.
博客来电子书自动转化简体中文-Tampermonkey
// ==UserScript==
// @name 博客来简体
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 博客来电子书自动转化简体中文
// @author lispczz
// @match https://viewer-ebook.books.com.tw/viewer/epub/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
console.log('博客来简体');
let globalConverterInstance = null;
function loadOpenCC() {
$.getScript("https://cdn.jsdelivr.net/npm/opencc-js@1.0.3/data.min.js", function() {
$.getScript("https://cdn.jsdelivr.net/npm/opencc-js@1.0.3/data.t2cn.min.js");
$.getScript("https://cdn.jsdelivr.net/npm/opencc-js@1.0.3/data.cn2t.min.js");
$.getScript("https://cdn.jsdelivr.net/npm/opencc-js@1.0.3/bundle-browser.min.js");
})
}
function convertNode(n, converter) {
const oldText = n.innerHTML;
const newText = converter(oldText);
n.innerHTML = newText;
}
function getConverter() {
if (typeof OpenCC == 'undefined') {
loadOpenCC();
return null;
}
if (globalConverterInstance == null) {
globalConverterInstance = OpenCC.Converter({ from: 'tw', to: 'cn' });
}
return globalConverterInstance;
}
function convert(rootNode) {
let converter = getConverter();
if (converter == null) {
console.log('博客来简体: converter not ready');
return;
}
if (rootNode == null) {
let frameDocument = document.getElementsByTagName('iframe')[0].contentDocument || document;
rootNode = frameDocument.getElementById("bodybox");
}
for (let n of rootNode.getElementsByTagName("p")) {
convertNode(n, converter);
}
}
const f = EPUBJS.Render.Iframe.prototype.loaded;
EPUBJS.Render.Iframe.prototype.loaded = function(v) {
f.bind(this)(v);
convert(this.bodyEl);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment