Skip to content

Instantly share code, notes, and snippets.

@Dither
Created September 14, 2016 07:55
Show Gist options
  • Save Dither/153c2c828b929d8f92054ed74256bd17 to your computer and use it in GitHub Desktop.
Save Dither/153c2c828b929d8f92054ed74256bd17 to your computer and use it in GitHub Desktop.
Dynasty Scans reader service script for AutoPatchWork Mod
// ==UserScript==
// @description dynasty-scans reader service script for AutoPatchWork mod.
// @include http://dynasty-scans.com/*
// ==/UserScript==
(function(){
document.addEventListener('DOMContentLoaded', function() {
var on, off, trigger;
var base_url = "http://dynasty-scans.com",
current_index = parseInt(location.hash.replace('#',''), 10) || 0,
is_new_chapterter = false,
next_chapter = null,
current_chapter = location.pathname;
function handleNewPageLink(event) {
is_new_chapterter = false;
if (current_index < pages.length - 1) {
event.detail.norequest = true;
current_index += 1;
} else {
is_new_chapterter = true;
current_index = 0;
if(next_chapter && next_chapter !== '#') {
event.detail.link = base_url + next_chapter;
} else {
off('request');
off('load');
event.preventDefault();
event.stopPropagation();
trigger('terminated', { message: 'Last chapter reached'});
}
}
}
function handleNewPageLoad(event) {
if (is_new_chapterter) {
var scripts = event.detail.htmlDoc.querySelectorAll('script');
for (var text = '', i = 0; i < scripts.length; i++) {
if (scripts[i].text.indexOf("var pages = ") !== -1) {
text = scripts[i].text.replace(/\s*\/\/<!\[CDATA\[\s*([\s\S]+)\s*\/\/\]\]>/gi, '$1');
is_new_chapterter = false;
current_chapter = event.detail.url;
next_chapter = event.detail.htmlDoc.querySelector('#next_link').getAttribute('href');
window.eval(text); //update pages variable for a new chapter
break;
}
}
} else {
event.detail.url = current_chapter+'#'+current_index;
}
event.detail.htmlDoc.body.innerHTML = '<div id="image"><a href="' + event.detail.url + '"><img src="' + pages[current_index].image + '" class="picture" /></a></div>';
var tl = event.detail.htmlDoc.querySelector('title');
if (!tl) {
tl = document.createElement('title');
event.detail.htmlDoc.head.appendChild(tl);
}
tl.textContent = current_chapter.replace(base_url, '').replace(/[_\/]/g,' ').replace(/chapters /g,' ').replace(/Dynasty Reader »\s*/i,'') + ' p.' + current_index;
}
document.addEventListener('AutoPatchWork.ready', function() {
trigger = AutoPatchWorked.trigger;
on = AutoPatchWorked.on;
off = AutoPatchWorked.off;
if (~location.href.indexOf('chapters')) {
on('request', handleNewPageLink);
on('load', handleNewPageLoad);
next_chapter = document.querySelector('#next_link').getAttribute('href');
trigger('siteinfo', {
siteinfo : {
url: "^http://dynasty-scans\.com/chapters",
pageElement: "id(\"image\")/*[self::img or self::a]",
nextLink: "id(\"next_link\")",
//disableSeparator: true,
forceIframe: false,
cssPatch: "#reader #image img { min-width: 700px; min-height:700px; }",
removeElement: "#loading,span.left,span.right"
}
});
} else {
trigger('siteinfo', {
siteinfo : {
url: "^http://dynasty-scans\.com",
pageElement: "//a[@class=\"chapter media thumbnail\"]",
nextLink: "//a[@rel=\"next\"]",
}
});
}
}, false);
}, false);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment