Skip to content

Instantly share code, notes, and snippets.

@Dither
Last active February 16, 2018 12:04
Show Gist options
  • Save Dither/d9db714517845941f0f380b61106e4d6 to your computer and use it in GitHub Desktop.
Save Dither/d9db714517845941f0f380b61106e4d6 to your computer and use it in GitHub Desktop.
Example of custom pager script for MangaWindow (original site's scripts should be disabled)
// ==UserScript==
// @description MangaWindow reader service script for AutoPatchWork mod.
// @include https://mangawindow.com/*
// ==/UserScript==
if (window.opera && ~location.pathname.indexOf('chapter')) {
// NSL error in Opera 12 caused by some random change in the site's code :(
/* // Additional CSS fixes:
.body-chapter .chapter-nav select, .body-chapter .chapter-nav select, .body-chapter .chapter-nav .nav-chas select {
color: #000;
}
.page-num {display:none}
*/
window.opera.addEventListener('BeforeScript', function(userJSEvent) {
userJSEvent.preventDefault();
}, false);
gotop = function(){};
}
(function(){
var on, off, trigger;
var base_url = 'https://mangawindow.com/chapter/',
current_index = 1,
current_chapter = null,
current_chapter_n = 0,
is_new_chapter = false,
next_chapter = null,
img_array = [],
null_chapter = null,
img_array_len = 0;
function copyTitleToClipboard() {
try {
var title = document.querySelector('.nav-title > a');
//title.textContent = title.textContent.replace(/ Man[gh][uw]?a/i,'');
if (!title) return;
var range = document.createRange();
range.selectNodeContents(title);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
document.execCommand('copy');
} catch (err) {}
}
function currentChapterLink(index) {
return current_chapter;
}
function updateChapterVariables(nodes, url, initial) {
var script_cue = 'var images = {',
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;
img_array = text.replace(/"\d+":/g,'').replace(/"/g,'').match(/var images\s*=\s*{([^}]+)}/)[1].split(',');
img_array_len = img_array.length;
current_index = 1;
current_chapter = url;
next_chapter = text.match(/var nextCha\s*=\s*[^\=]+uniqueId.:(\d+)/);
next_chapter = next_chapter ? next_chapter[1] : '';
current_chapter_n = text.match(/var prevCha\s*=\s*[^\=]+volcha":"[^\d"]+(\d+)/);
if (current_chapter_n && current_chapter_n[1]) {
current_chapter_n = parseInt(current_chapter_n[1], 10) + 1;
} else {
current_chapter_n = text.match(/var nextCha\s*=\s*[^\=]+"volcha":"[^\d"]+(\d+)/);
if (current_chapter_n && current_chapter_n[1]) {
current_chapter_n = parseInt(current_chapter_n[1], 10) - 1;
} else {
current_chapter_n = 1;
}
}
if (initial) current_index = img_array_len;
break;
}
}
}
function generateBody(index, url) {
return '<div id=\"viewer\"><div class="item"><img style=\"max-width:1100px;display:block;\" src=\"' + img_array[index-1] + '\" /></div></div>';
}
function generateTitle(page, title, chapter, volume) {
return (chapter ? (' ch.' + chapter + ' ') : '') + 'p.' + page + ' of ' + title;
}
function terminateAPW(event) {
console.log('[APW; mangawindow]', 'terminated');
trigger('terminated', { message: 'Last chapter reached'});
off('request');
off('load');
if (!event) return;
event.preventDefault();
event.stopPropagation();
}
function handleNewPageLink(event) {
is_new_chapter = false;
current_index += 1;
if (!img_array_len) return;
if (current_index <= img_array_len) {
event.detail.norequest = true;
} else {
is_new_chapter = true;
if (isChapter(next_chapter)) event.detail.link = base_url + next_chapter + '/';
else { event.detail.link = ''; terminateAPW(event); }
}
}
function handleNewPageLoad(event) {
if (is_new_chapter) {
updateChapterVariables(event.detail.htmlDoc, event.detail.url, false);
} else {
event.detail.url = currentChapterLink(current_index);
}
event.detail.htmlDoc.body.innerHTML = img_array[current_index-1] ? generateBody(current_index, event.detail.url) : null;
var tl = event.detail.htmlDoc.querySelector('title');
if (!tl) {
tl = document.createElement('title');
event.detail.htmlDoc.head.appendChild(tl);
}
tl.textContent = generateTitle(current_index, document.querySelector('.nav-title a').textContent, current_chapter_n);
}
function isChapter(chapter) {
return chapter && chapter.length && chapter !== null_chapter && (chapter.indexOf('javascript:') === -1);
}
function initChapter() {
var container = document.querySelector('#viewer');
for (var i = 0; i < img_array_len; i++) {
var img = document.createElement('img'),
div = document.createElement('div');
div.className = "item";
img.src = img_array[i];
img.style = "max-width:1100px;display:block;";
div.appendChild(img);
container.appendChild(div);
}
var tl = document.querySelector('title');
if (!tl) {
tl = document.createElement('title');
document.head.appendChild(tl);
}
tl.textContent = generateTitle(current_index, document.querySelector('.nav-title a').textContent, current_chapter_n);
}
document.addEventListener('AutoPatchWork.ready', function() {
console.log('[APW; mangawindow]', 'helper script loaded');
trigger = window.AutoPatchWorked.trigger;
on = window.AutoPatchWorked.on;
off = window.AutoPatchWorked.off;
if (!document.querySelectorAll('#viewer').length) {
// block native autopager sript `latest.mod.js` for this to work
// also this to prevent any jitter:
if (window.opera) {
window.opera.addEventListener('BeforeScript', function(e) {
if (~userJSEvent.element.src.indexOf('anycg')) e.preventDefault();
}, false);
window.scrollBy = window.scrollTo = function(){};
}
trigger('siteinfo', {
siteinfo : {
url: '^https://mangawindow\\.com/latest',
pageElement: '.item.col-24',
nextMask: 'https://mangawindow.com/latest?page=|1|',
forceAddressChange: true // my default setting is enabled, this reverts it
}
});
} else {
on('request', handleNewPageLink);
on('load', handleNewPageLoad);
updateChapterVariables(document, location.href, true);
if (!!window.opera) initChapter();
if (!isChapter(next_chapter)) return;
trigger('siteinfo', {
siteinfo : {
url: '^https://mangawindow\\.com/chapter',
pageElement: 'id(\"viewer\")/*',
nextLink: '//a',
disableSeparator: true,
cssPatch: ".item>img {min-height:500px;}"
}
});
}
}, false);
//if (typeof browser !== 'undefined')
document.addEventListener('DOMContentLoaded', function() {
copyTitleToClipboard();
//window.AutoPatchWorked.trigger('ready');
}, false);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment