Skip to content

Commit

Permalink
Merge branch 'ExperimentalTabMode' into MagicWizards
Browse files Browse the repository at this point in the history
  • Loading branch information
gamebeaker authored Sep 25, 2024
2 parents 6ee4970 + fafcee8 commit 1905f9d
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 58 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ eslint/*.zip
eslint/packed.js
eslint/index.csv
node_modules
!plugin/**/*.*
plugin/jszip/dist/jszip.min.js
package-lock.json
package-lock.json
48 changes: 28 additions & 20 deletions plugin/js/Library.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ class Library {
constructor() {
}

onUserPreferencesUpdate(userPreferences) {
Library.userPreferences = userPreferences;
}

LibAddToLibrary(AddEpub, fileName, overwriteExisting, backgroundDownload){
if (document.getElementById("includeInReadingListCheckbox").checked != true) {
document.getElementById("includeInReadingListCheckbox").click();
}
chrome.storage.local.get(null, async function(items) {
let CurrentLibStoryURLKeys = await Library.LibGetAllLibStorageKeys("LibStoryURL");
let CurrentLibStoryURLKeys = await Library.LibGetAllLibStorageKeys("LibStoryURL", Object.keys(items));
let LibidURL = -1;
for (let i = 0; i < CurrentLibStoryURLKeys.length; i++) {
if (items[CurrentLibStoryURLKeys[i]] == document.getElementById("startingUrlInput").value) {
Expand Down Expand Up @@ -242,18 +246,16 @@ class Library {

static Libdeleteall(){
chrome.storage.local.get(null, async function(items) {
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub", Object.keys(items));
let storyurls = [];
for (let i = 0; i < CurrentLibKeys.length; i++) {
CurrentLibKeys[i] = CurrentLibKeys[i].replace("LibEpub","");
}
for (let i = 0; i < CurrentLibKeys.length; i++) {
storyurls[i] = items["LibStoryURL" + CurrentLibKeys[i]];
}
let userPreferences = new UserPreferences;
userPreferences = UserPreferences.readFromLocalStorage();
for (let i = 0; i < storyurls.length; i++) {
userPreferences.readingList.tryDeleteEpubAndSave(storyurls[i]);
Library.userPreferences.readingList.tryDeleteEpubAndSave(storyurls[i]);
}
chrome.storage.local.clear();
Library.LibRenderSavedEpubs();
Expand All @@ -263,7 +265,7 @@ class Library {
static LibRenderSavedEpubs(){
chrome.storage.local.get(null, async function(items) {
let ShowAdvancedOptions = document.getElementById("LibShowAdvancedOptionsCheckbox").checked;
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub", Object.keys(items));
let LibRenderResult = document.getElementById("LibRenderResult");
let LibRenderString = "";
let LibTemplateDeleteEpub = document.getElementById("LibTemplateDeleteEpub").innerHTML;
Expand Down Expand Up @@ -646,9 +648,7 @@ class Library {

static LibDeleteEpub(objbtn){
let LibRemove = ["LibEpub" + objbtn.dataset.libepubid, "LibStoryURL" + objbtn.dataset.libepubid, "LibFilename" + objbtn.dataset.libepubid, "LibCover" + objbtn.dataset.libepubid];
let userPreferences = new UserPreferences;
userPreferences = UserPreferences.readFromLocalStorage();
userPreferences.readingList.tryDeleteEpubAndSave(document.getElementById("LibStoryURL" + objbtn.dataset.libepubid).value);
Library.userPreferences.readingList.tryDeleteEpubAndSave(document.getElementById("LibStoryURL" + objbtn.dataset.libepubid).value);
chrome.storage.local.remove(LibRemove);
Library.LibRenderSavedEpubs();
}
Expand Down Expand Up @@ -682,7 +682,7 @@ class Library {

static Libexportall(){
chrome.storage.local.get(null, async function(items) {
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub");
let CurrentLibKeys = await Library.LibGetAllLibStorageKeys("LibEpub", Object.keys(items));
var retobj = {};
retobj.Library = [];
for (let i = 0; i < CurrentLibKeys.length; i++) {
Expand Down Expand Up @@ -738,9 +738,7 @@ class Library {
});
HighestLibEpub++;
}
let userPreferences = new UserPreferences;
userPreferences = UserPreferences.readFromLocalStorage();
userPreferences.loadReadingListFromJson(json);
Library.userPreferences.loadReadingListFromJson(json);
Library.LibRenderSavedEpubs();
}

Expand All @@ -762,18 +760,28 @@ class Library {
document.getElementById("LibURLWarning"+obj.dataset.libepubid).innerHTML = "<tr><td></td></tr>";
}

static async LibGetAllLibStorageKeys(Substring){
static async LibGetAllLibStorageKeys(Substring, AllStorageKeysList){
return new Promise((resolve) => {
chrome.storage.local.get(null, function(items){
let AllStorageKeys = Object.keys(items);
if (AllStorageKeysList == undefined) {
chrome.storage.local.get(null, function(items){
let AllStorageKeys = Object.keys(items);
let AllLibStorageKeys = [];
for (let i = 0, end = AllStorageKeys.length; i < end; i++) {
if(AllStorageKeys[i].includes(Substring)){
AllLibStorageKeys.push(AllStorageKeys[i]);
}
}
resolve(AllLibStorageKeys);
});
} else {
let AllLibStorageKeys = [];
for (let i = 0, end = AllStorageKeys.length; i < end; i++) {
if(AllStorageKeys[i].includes(Substring)){
AllLibStorageKeys.push(AllStorageKeys[i]);
for (let i = 0, end = AllStorageKeysList.length; i < end; i++) {
if(AllStorageKeysList[i].includes(Substring)){
AllLibStorageKeys.push(AllStorageKeysList[i]);
}
}
resolve(AllLibStorageKeys);
});
}
});
}

Expand Down
1 change: 1 addition & 0 deletions plugin/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ var main = (function () {

function loadUserPreferences() {
userPreferences = UserPreferences.readFromLocalStorage();
userPreferences.addObserver(library);
userPreferences.writeToUi();
userPreferences.hookupUi();
BakaTsukiSeriesPageParser.registerBakaParsers(userPreferences.autoSelectBTSeriesPage.value);
Expand Down
29 changes: 29 additions & 0 deletions plugin/js/parsers/Book18Parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Parses files on www.book18.org
*/
"use strict";

parserFactory.register("book18.org", () => new Book18Parser());

class Book18Parser extends Parser{
constructor() {
super();
}

async getChapterUrls(dom) {
let menu = dom.querySelector(".list-group");
return util.hyperlinksToChapterList(menu);
}

findContent(dom) {
return dom.querySelector("div#content");
}

extractTitleImpl(dom) {
return dom.querySelector("title");
}

removeUnwantedElementsFromContentElement(element) {
util.removeChildElementsMatchingCss(element, "span.d-none");
}
}
36 changes: 36 additions & 0 deletions plugin/js/parsers/NovelfullParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ class NovelfullParser extends Parser{
: super.extractAuthor(dom);
}

preprocessRawDom(dom) {
this.tagWatermark(dom);
}

findChapterTitle(dom) {
return dom.querySelector("h2").textContent;
}
Expand All @@ -132,6 +136,38 @@ class NovelfullParser extends Parser{
getInformationEpubItemChildNodes(dom) {
return [...dom.querySelectorAll("div.desc-text, div.info")];
}

tagWatermark(dom) {
const watermark = this.findWatermark(dom);
if (watermark) {
let paragraphs = [...dom.querySelectorAll("p")]
.filter(p => p.textContent.includes(watermark));
for(let p of paragraphs) {
p.textContent = p.textContent.replace(watermark, "");
p.appendChild(this.makeSpanWithWatermark(dom, watermark));
}
}
}

findWatermark(dom) {
const searchToken = "original11Content.replace(\"";
const script = [...dom.querySelectorAll("script")]
.filter(s => s.innerHTML.includes(searchToken))
.map(s => s.innerHTML)[0];
if (!script) {
return null;
}
const line = script.substring(script.indexOf(searchToken) + searchToken.length);
return line.substring(0, line.indexOf("\""));
}

makeSpanWithWatermark(dom, watermark) {
let span = dom.createElement("span");
span.textContent = watermark;
span.id = "span";
span.hidden = true;
return span;
}
}

class Novel35Parser extends NovelfullParser{
Expand Down
46 changes: 11 additions & 35 deletions plugin/js/parsers/SyosetuParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ class SyosetuParser extends Parser{
}

async getChapterUrls(dom, chapterUrlsUI) {
await this.fetchAndAttachInfoPage(dom);
return this.getChapterUrlsFromMultipleTocPages(dom,
this.extractPartialChapterList,
this.getUrlsOfTocPages,
chapterUrlsUI
);
}

async fetchAndAttachInfoPage(dom) {
const infoPageUrl = dom.querySelector("#head_nav > li:nth-child(2) > a").href;
this.infoPageDom = (await HttpClient.fetchHtml(infoPageUrl)).responseXML; // Parse and store the info page DOM
}

getUrlsOfTocPages(dom) {
let lastPage = dom.querySelector("a.novelview_pager-last");
let lastPage = dom.querySelector("a.c-pager__item--last");
let urls = [];
if (lastPage) {
const lastPageNumber = parseInt(lastPage.href.split("?p=")[1]);
Expand All @@ -37,20 +31,20 @@ class SyosetuParser extends Parser{
}

extractPartialChapterList(dom) {
let chapterList = dom.querySelector("div.index_box") || dom.querySelector("div.novel_sublist");
let chapterList = dom.querySelector("div.index_box") || dom.querySelector("div.p-eplist");
return [...chapterList.querySelectorAll("a")].map(a => util.hyperLinkToChapter(a));
}

findContent(dom) {
return dom.querySelector("div#novel_honbun");
return dom.querySelector("div.p-novel__body");
};

extractTitleImpl(dom) {
return dom.querySelector(".novel_title");
return dom.querySelector(".p-novel__title");
};

extractAuthor(dom) {
const authorDiv = dom.querySelector("div.novel_writername");
const authorDiv = dom.querySelector("div.p-novel__author");
if (authorDiv) {
const authorText = authorDiv.textContent.trim().replace(/^作者:/, "");
return authorDiv.querySelector("a")?.textContent.trim() || authorText;
Expand All @@ -59,33 +53,15 @@ class SyosetuParser extends Parser{
}

findChapterTitle(dom) {
let element = dom.querySelector(".novel_subtitle");
let element = dom.querySelector(".p-novel__title");
return (element === null) ? null : element.textContent;
}

getInformationEpubItemChildNodes() {
const infoNodes = [];
const infoTable = this.infoPageDom.querySelector("#infodata");
const infoTableClone = infoTable.cloneNode(true);
infoNodes.push(infoTableClone);
return infoNodes;
getInformationEpubItemChildNodes(dom) {
return [...dom.querySelectorAll("h1.p-novel__title, #novel_ex")];
}

cleanInformationNode(node) {
util.removeChildElementsMatchingCss(node, "#qr, #pre_info > a");
const preInfoDiv = node.querySelector("#pre_info");
this.removePipeCharacter(preInfoDiv);
let sibling = preInfoDiv.querySelector("#noveltype_notend")?.nextSibling;
if (sibling) {
sibling.textContent = sibling.textContent.replace(/^全/, " 全");
}
}

removePipeCharacter(contentElement) {
let walker = contentElement.ownerDocument.createTreeWalker(contentElement, NodeFilter.SHOW_TEXT);
while (walker.nextNode()) {
let node = walker.currentNode;
node.textContent = node.textContent.replace(/\|/g, "");
};
}
extractDescription(dom) {
return dom.querySelector(".p-novel__summary").textContent.trim();
}
}
2 changes: 1 addition & 1 deletion plugin/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "WebToEpub",
"version": "0.0.0.193",
"version": "0.0.0.200",
"default_locale": "en",
"icons": {
"128": "book128.png"
Expand Down
1 change: 1 addition & 0 deletions plugin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ <h3>Instructions</h3>
<script src="js/parsers/BakaTsukiSeriesPageParser.js"></script>
<script src="js/parsers/BetwixtedbutterflyParser.js"></script>
<script src="js/parsers/BlogspotParser.js"></script>
<script src="js/parsers/Book18Parser.js"></script>
<script src="js/parsers/BookswithqianyaParser.js"></script>
<script src="js/parsers/Booktoki152Parser.js"></script>
<script src="js/parsers/BotitranslationParser.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions unitTest/Tests.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<script src="../plugin/js/parsers/MangadexParser.js"></script>
<script src="../plugin/js/parsers/MangaHereParser.js"></script>
<script src="../plugin/js/parsers/MuggleNetParser.js"></script>
<script src="../plugin/js/parsers/NovelfullParser.js"></script>
<script src="../plugin/js/parsers/NovelSpreadParser.js"></script>
<script src="../plugin/js/parsers/NovelUniverseParser.js"></script>
<script src="../plugin/js/parsers/NovelUpdatesParser.js"></script>
Expand Down Expand Up @@ -124,6 +125,7 @@
<script src="UtestMuggleNetParser.js"></script>
<script src="UtestNepustationParser.js"></script>
<script src="UtestNovelSpreadParser.js"></script>
<script src="UtestNovelfullParser.js"></script>
<script src="UtestNovelUpdatesParser.js"></script>
<script src="UtestQidianParser.js"></script>
<script src="UtestQinxiaoshuoParser.js"></script>
Expand Down
57 changes: 57 additions & 0 deletions unitTest/UtestNovelfullParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

"use strict";

module("NovelfullParser");

QUnit.test("findWatermark", function (assert) {
let dom = new DOMParser().parseFromString(NovelfullSample, "text/html");
let parser = new NovelfullParser();
let watermark = parser.findWatermark(dom);
assert.equal(watermark, "n/ô/vel/b//jn dot c//om");
});

QUnit.test("tagWatermark", function (assert) {
let dom = new DOMParser().parseFromString(NovelfullSample, "text/html");
let parser = new NovelfullParser();
parser.tagWatermark(dom);
let paragraph = dom.querySelector("#watermarked");
let span = paragraph.querySelector("span");
assert.equal(span.innerHTML, "n/ô/vel/b//jn dot c//om");
assert.equal(paragraph.childNodes[0].nodeValue, " Yuan found their wording quite weird, but who was he to judge their world? ");
});

let NovelfullSample =
`<!DOCTYPE html>
<html lang="en">
<head>
<title>Cultivation Online #Chapter 1596 Primal Expanse - Read Cultivation Online Chapter 1596 Primal Expanse Online - All Page - Novel Bin</title>
</head>
<body>
<div id="chr-content">
<div id="pf-10311-1">
<script>window.pubfuturetag = window.pubfuturetag || [];window.pubfuturetag.push({unit: "66b4e3c40939a022784366eb", id: "pf-10311-1"})</script></div>
<div></div>
<h3>Chapter 1596 &nbsp;Primal Expanse</h3> <p> </p><p> After taking a moment to digest the possibility that they had been transported to another world outside the Nine Heavens, Yuan turned to look at the mysterious naked little girl and asked, "Do you mind telling us a little about the Primal Expanse?" </p><div id="pf-10364-1">
<script>window.pubfuturetag = window.pubfuturetag || [];window.pubfuturetag.push({unit: "66b9b2575d6f5a59dab6ff6d", id: "pf-10364-1"})</script></div><p id="watermarked"> Yuan found their wording quite weird, but who was he to judge their world? n/ô/vel/b//jn dot c//om</p><p> "Primal and Predators, right? I will remember that." </p>
<div id="pf-10366-1">
<script>window.pubfuturetag = window.pubfuturetag || [];window.pubfuturetag.push({unit: "66b9b27899ef0d23774745cd", id: "pf-10366-1"})</script></div>
</div>
<script>
setTimeout(function () {
const paragraphss = $("p");
paragraphss.each(function () {
const original11Content = $(this).html();
const updated11Content = original11Content.replace("n/ô/vel/b//jn dot c//om", \`<span id="span">n/ô/vel/b//jn dot c//om</span>\`);
$(this).html(updated11Content);
});
}, 600000);
</script>
</body>
</html>`

0 comments on commit 1905f9d

Please sign in to comment.