Skip to content

Instantly share code, notes, and snippets.

@Dither
Last active June 7, 2018 23:33
Show Gist options
  • Save Dither/cadf33822c2c13b525d2b6919e153d2e to your computer and use it in GitHub Desktop.
Save Dither/cadf33822c2c13b525d2b6919e153d2e to your computer and use it in GitHub Desktop.
MangaDex reader service script for AutoPatchWork mod
// ==UserScript==
// @description MangaDex reader service script for AutoPatchWork mod.
// @include https://mangadex.org*
// ==/UserScript==
(function(){
var on, off, trigger;
var base_url = '',
chapter_url = 'https://mangadex.org/chapter/',
data_url = '',
is_webtoon = false,
manga_title = 'Unknown',
manga_chapter = 0,
current_index = 0,
current_chapter = null,
current_chapter_n = 0,
is_new_chapter = false,
next_chapter = null,
img_array = [],
null_chapter = 0,
img_array_len = 0;
function copyTitleToClipboard() {
var title = document.querySelector('.panel-title > a');
if (!title) return;
var range = document.createRange();
range.selectNodeContents(title);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
document.execCommand('copy');
}
function updateChapterVariables(nodes, url, initial) {
var script_cue = 'var prev_chapter_id',
scripts = nodes.querySelectorAll('script');
//serach all embedded scripts for matching array and chapter data
for (var text = '', i = 0; i < scripts.length; i++) {
if (scripts[i].text.indexOf(script_cue) !== -1) {
text = scripts[i].text;
is_new_chapter = false;
base_url = text.match(/var server = '([^']+)'/)[1];
data_url = text.match(/var dataurl = '([^']+)'/)[1];
img_array = text.replace(/'/g,'').match(/var page_array = \[([^\]]+),\]/)[1].split(',');
img_array_len = img_array.length;
current_index = 1;
next_chapter = text.match(/var next_chapter_id = (\d+)/);
next_chapter = next_chapter ? next_chapter[1] : 0;
current_chapter = text.match(/var chapter_id = (\d+)/);
current_chapter = current_chapter ? current_chapter[1] : 0;
break;
}
}
}
function generateBody(index, url) {
return '<div><img id=\"current_page\" class=' + (is_webtoon ? '\"webtoon click\"' : '\"reader max-width\"') + ' src=\"'+
base_url + data_url + '/' + img_array[index - 1] +
'\" alt=\"image\" data-page=\"' + index + '\"/></div>';
}
function generateTitle(title, index, chapter) {
return (!!chapter ? (' ch.' + chapter + ' ') : '') + 'p.' + index + ' of ' + title;
}
function updateTitle(doc) {
var tl = doc.querySelector('title');
if (!tl) {
tl = document.createElement('title');
doc.head.appendChild(tl);
}
tl.textContent = generateTitle(manga_title, current_index, manga_chapter);
}
function getTitle(doc) {
var tl = doc.querySelector('[property=\"og:title\"]');
if (tl) {
var title = tl.content.match(/(?:Ch\. ([\d.]+) )?\((.+)\) - MangaDex/);
if (title) {
manga_title = title[2] ? title[2] : 'Unknown';
manga_chapter = title[1] ? title[1].replace('.','-') : '';
}
}
}
function terminateAPW(event) {
if (event) {
if (event.detail) event.detail.link = null;
event.preventDefault();
event.stopPropagation();
}
trigger('terminated', { message: 'last chapter reached'});
off('request');
off('load');
copyTitleToClipboard();
}
function handleNewPageLink(event) {
is_new_chapter = false;
current_index += 1;
if (!img_array_len) { terminateAPW(event); return; }
if (current_index <= img_array_len) {
event.detail.norequest = true;
} else {
is_new_chapter = true;
if (isChapter(next_chapter)) event.detail.link = chapter_url + next_chapter + '/' + '1';
else { terminateAPW(event); }
}
}
function handleNewPageLoad(event) {
if (is_new_chapter) {
getTitle(event.detail.htmlDoc);
updateChapterVariables(event.detail.htmlDoc, event.detail.url, false);
} else {
event.detail.url = chapter_url + current_chapter + '/' + current_index;
}
event.detail.htmlDoc.body.innerHTML = img_array[current_index-1] ? generateBody(current_index, event.detail.url) : null;
updateTitle(event.detail.htmlDoc);
}
function isChapter(chapter) {
return chapter && chapter.length && chapter != null_chapter;
}
document.addEventListener('AutoPatchWork.ready', function() {
trigger = window.AutoPatchWorked.trigger; on = window.AutoPatchWorked.on; off = window.AutoPatchWorked.off;
is_webtoon = !!document.querySelectorAll('img.webtoon.click').length;
if (is_webtoon || document.querySelectorAll('#current_page').length) {
console.log('[APW; mangadex]', 'reader helper script loaded');
updateChapterVariables(document, '', true);
current_index = is_webtoon ? img_array_len : parseInt(location.pathname.match(/chapter\/\d+\/(\d+)/)[1], 10);
getTitle(document);
updateTitle(document, true);
on('request', handleNewPageLink);
on('load', handleNewPageLoad);
trigger('siteinfo', {
siteinfo : {
url: '^https://mangadex\\.org/chapter',
pageElement: '#current_page, img.webtoon.click',
nextLink: 'a[href]',
disableSeparator: true, //is_webtoon,
cssPatch: 'img.reader { margin: 0 auto; } body { padding-top: 0!important; } nav {position:static!important;} body {background: #111!important; color: #aaa!important; border:0!important;}'
//forceAddressChange: true // my default setting is enabled, this reverts it
}
});
} else {
console.log('[APW; mangadex]', 'pager helper script loaded');
trigger('siteinfo', {
siteinfo : {
url: '^https://mangadex\\.org',
pageElement: '.table-responsive .table tr',
nextLink: 'li.active + .paging > a',
forceAddressChange: true // my default setting is enabled, this reverts it
}
})
}
}, false);
//if (typeof browser !== 'undefined')
document.addEventListener('DOMContentLoaded', function() {
//window.AutoPatchWorked.trigger('ready');
}, false);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment