// ==UserScript==
// @name Mangadex Plus
// @namespace https://greasyfork.org/users/553660
// @icon https://mangadex.org/images/misc/navbar.svg
// @version 1.0
// @description Adds a few useful features to Mangadex
// @author Mr. M
// @match https://mangadex.org/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_listValues
// @grant GM_deleteValue
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
/* TODO
- Show managa cover on hover
*/
/*
Global values
*/
var domain = [
"mangadex.org"
]
var url = window.location;
var nVer = "1.0";
/*
Global functions
*/
// Get the domain from the visiting website
function url_domain(data) {
var a = document.createElement('a');
a.href = data;
return a.hostname;
}
// Add a global CSS style by inputting a String
function addGlobalStyle(css) {
var head, style;
head = document.getElementsByTagName('head')[0];
if (!head) { return; }
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = css;
head.appendChild(style);
}
// Get site subpage name/directory from provided url
function getSubPage(directoryIndex){
let url = document.location.href;
var segment = url.replace(/^https?:\/\//, '').split('/')[directoryIndex];
return segment;
}
// Get current Url
function currentUrl(){
return document.location.href;
}
/*
Storage defaults
*/
let first_time;
if (GM_getValue("first_time") != "false"){
first_time = false;
GM_setValue("first_time", "false");
//GM_setValue("folders_index", 0);
let folders = {"folders" : []};
GM_setValue("folders", JSON.stringify(folders));
let options = {"options" : [{"preloading" : 0}]};
GM_setValue("options", JSON.stringify(options));
GM_setValue("update_notif", "true");
alert("Thank you for installing Mangadex Plus! Go to Settings -> About to learn more about Mangadex Plus!")
}
// Version check (last version / newest version)
if (GM_getValue("version") != nVer && first_time == undefined){
GM_setValue("version", nVer);
GM_setValue("update_notif", "true")
alert("A feature update is here! MD+ has been updated to " + GM_getValue("version") + "! Check Change Log in MD+ Settings for more info!");
}
var red_badge;
if (GM_getValue("update_notif") == "true"){
GM_setValue("notif_badge", ' ! ')
}
else{
GM_setValue("notif_badge", '')
}
/*
Site functions
*/
// Startup functions
function startup(){
if (getSubPage(1) == "title"){
main("title");
}
else if (getSubPage(1) == "chapter"){
main("chapter")
}
else{
main("other");
}
}
/*
HTML Elements
*/
// Color categorizing CSS classes -> "badge" || "text" + "-" + "success"/"danger"/"primary"/nothing
//let friends_icon = '';
//red_badge = ' ! ';
//let green_badge = ' ! ';
//let msg_icon = '';
let container = document.getElementsByTagName("body")[0];
let pageMask = '
';
let closeButton = ' To Exit click anywhere or press Esc.';
/*
Functions and features
*/
function main(level){
let menuButton = '
" +
"\"What is Mangadex Plus?\" Mangadex Plus is a Userscript that adds some useful features to Mangadex.
" +
"\"What are these Useful features you speak of?\" These currently include:" +
"
Custom folders
Marking all chapters of a Manga as read/unread
Start reading button
" +
"
Automatic Chapter Preloading
" +
"
For more info on the features head to the Change Log. As of now these are all but more features will be added through time.
" +
"\"I really want a feature to be added to this script! Do you take suggestions?\" I do so Welcomely. Please go to the GreasyFork page of this Userscript and add a suggestion there.
" +
"\"Hey! I found a bug! Where can I report It?\" Please report bugs on the GreasyFork page of this Userscript and I'll do my best to fix them ASAP.
" +
'
To remove the red badge ( ! ) please reload the page.';
let element = '
" +
"The very first version brings these features to the table:" +
"
Custom folders: You can make folders and add your chosen titles to It.
Mark all chapters of a Manga as read/unread: " +
"Saves you the trouble of having to mark all chapters individually.
Start reading button: Goes to the first chapter of a Manga without having to scroll or switch pages.
" +
"
Automatic Chapter Preloading: Whenever you are in the reader (and are logged into your MD Account) the chapter will automatically start preloading. " +
"You can turn this feature on/off in th Options Menu.
" +
"I am planning on improving the current features and adding new ones. If you have a suggestion/bug report please let me know on the Userscript's GreasyFork page forum.";
let element = '
' +
'
' +
'
' +
' Change Log
' +
'
' +
'
' + text + '
' +
'' +
'
';
closeMenu();
container.insertAdjacentHTML("afterbegin", element);
addGlobalStyle("div.plus-box { position: fixed !important; right: 700px !important; left: 700px !important; top: 100px !important; z-index: 100001;}");
container.insertAdjacentHTML("afterbegin", pageMask);
addGlobalStyle("#page-mask { background: rgba(0, 0, 0, 0.5); position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 100000;}");
container.insertAdjacentHTML("afterbegin", closeButton);
addGlobalStyle(".plus-clsBtn { right: 150px !important; position: fixed !important; z-index: 100000; width: auto; color: white; background-color: #444;}");
document.getElementsByClassName("plus-back-settings")[0].addEventListener("click", function(){closeMenu(); openSettingsMenu();});
document.getElementById("page-mask").addEventListener("click", closeMenu);
document.getElementsByClassName("plus-clsBtn")[0].addEventListener("click", closeMenu);
document.getElementsByClassName("plus-close")[0].addEventListener("click", closeMenu);
}
function resetMDP(level){
function prompt(level){
let text_rw = "WARNING: By confirming this you will DELETE ALL Your Mangadex Plus saved data! This includes options and folders! \nAre you sure you want to proceed?";
let text_r = "WARNING: By confirming this you will RESET ALL Your Mangadex Plus options to Default values! \nAre you sure you want to proceed?";
let text_w = "WARNING: By confirming this you will WIPE ALL Your Mangadex Plus folders and It's Entries! \nAre you sure you want to proceed?";
if (level == "rw"){
if (confirm(text_rw)) {
resets("rw");
}
}
else if (level == "r"){
if (confirm(text_r)) {
resets("r");
}
}
else if (level == "w"){
if (confirm(text_w)) {
resets("w");
}
}
}
prompt(level);
}
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '27' || e.keyCode == '27') {
closeMenu();
}
}
document.onkeydown = checkKey;
document.getElementsByClassName("plus-settings")[0].addEventListener("click", openSettingsMenu);
}
function folders(){
function getFolders(){
let folders = (JSON.parse(GM_getValue("folders"))).folders;
let backFolder = '
..
'
let createFolder = '
Create New Folder
'
let otherFolders = "";
let edit_btn = '
' +
'
';
let delete_btn = '
' +
'
';
for (let i = 0; i < folders.length; i++){
otherFolders = otherFolders + '
' +
' ' + folders[i].name + '
' + edit_btn + delete_btn + '
';
}
return backFolder + otherFolders + createFolder;
}
function folderItems(index){
let backFolder = '
..
'
let folders = (JSON.parse(GM_getValue("folders"))).folders;
let folder_name = (JSON.parse(GM_getValue("folders"))).folders[index].name;
let delete_btn = '
' +
'
';
let html = "";
for (let i = 0; i < (folders[index].entries).length; i++){
html = html + '
(*) Automatically starts preloading chapter when in the reader. Only works for logged-in users.
'
let options = //'
' +
'
' +
'
' +
'
' +
' Mangadex Plus Options' +
'
' +
'' +
'
' +
'
' +
'' +
'
' +
'' +
'
';
//'
';
function listeners(){
//Listeners for options buttons
for(let i = 0; i < document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a").length; i++){
document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a")[i].addEventListener("click", function(){selectOption("preloading", i)});
}
document.getElementsByClassName("plus-options-save")[0].addEventListener("click", saveOptions);
}
function openOptionsMenu(){
container.insertAdjacentHTML("afterbegin", options);
addGlobalStyle("div.plus-box { position: fixed !important; right: 700px !important; left: 700px !important; top: 100px !important; z-index: 100001;}");
container.insertAdjacentHTML("afterbegin", pageMask);
addGlobalStyle("#page-mask { background: rgba(0, 0, 0, 0.5); position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 100000;}");
container.insertAdjacentHTML("afterbegin", closeButton);
addGlobalStyle(".plus-clsBtn { right: 150px !important; position: fixed !important; z-index: 100000; width: auto; color: white; background-color: #444;}");
document.getElementById("page-mask").addEventListener("click", closeMenu);
document.getElementsByClassName("plus-clsBtn")[0].addEventListener("click", closeMenu);
document.getElementsByClassName("plus-close")[0].addEventListener("click", closeMenu);
loadOptions();
listeners();
}
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '27' || e.keyCode == '27') {
closeMenu();
}
}
let sample_options = {
"options" : [
{
"preloading" : 0
}
]
}
function saveOptions(){
GM_setValue("options", "{\"options\":[" + JSON.stringify(temp_storage) + "]}")
location.reload()
}
function loadOptions(){
var options = JSON.parse(GM_getValue("options"));
let preloading = options.options[0].preloading;
selectOption("preloading", preloading);
}
function selectOption(option, index){
if(option == "preloading"){
let val;
for(let i = 0; i < document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a").length; i++){
try{
document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a")[i].classList.remove('selected');
document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a")[i].classList.remove('active');
val = document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a")[index].childNodes[1].innerHTML;
}
catch(e){/*Nothing*/}
}
document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a")[index].classList.add("active");
document.getElementsByClassName("plus-preloading-options")[0].getElementsByTagName("a")[index].classList.add("selected");
document.getElementsByClassName("plus-preloading-selected")[0].innerHTML = val;
temp_storage.preloading = index;
}
}
document.onkeydown = checkKey;
document.getElementsByClassName("plus-options")[0].addEventListener("click", openOptionsMenu);
}
function actionsBar(){
function makeBar(){
let bar = '
' +
'
Mangadex+ Actions:
' +
'
' +
'
' +
'
';
var position = document.getElementsByClassName("col-xl-9 col-lg-8 col-md-7")[0];
position.insertAdjacentHTML("beforeend", bar);
}
function addToFolder(){
function addEntry(index){
let folders = JSON.parse(GM_getValue("folders"));
let link = ("" + window.location).replace("#", "");
let pass = true;
for(let i = 0; i < folders.folders[index].entries.length; i++){
try{
if(link == folders.folders[index].entries[0].link){
pass = false;
}
}
catch(e){
if (e == TypeError){
pass = true;
}
}
}
if (pass == true){
folders.folders[index].entries[folders.folders[index].entries.length] = {"title": "" + document.getElementsByClassName("card-header")[0].childNodes[3].innerHTML, "link" : "" + link};
GM_setValue("folders","" + JSON.stringify(folders));
}
}
function listFolders(){
let folders = JSON.parse(GM_getValue("folders"));
let html = "";
for (let i = 0; i < folders.folders.length; i++){
html = html + '
' + folders.folders[i].name + '
';
}
return html;
}
let bar = '
' +
'' +
'
' +
listFolders() +
'
' +
'
 ';
let position2 = document.getElementsByClassName("plus-actionbar")[0];
position2.insertAdjacentHTML("beforeend", bar);
let folders = JSON.parse(GM_getValue("folders"));
for (let i = 0; i < folders.folders.length; i++){
document.getElementsByClassName("plus-add-folder-" + i)[0].addEventListener("click", function(){addEntry(i)});
}
function reloadFolders(){
let index = document.getElementsByClassName("plus-actions-folders")[0].childNodes.length
for(let i = 0; i < index; i++){
document.getElementsByClassName("plus-add-folder-" + i)[0].remove();
}
let position3 = document.getElementsByClassName("plus-actions-folders")[0];
position3.insertAdjacentHTML("afterbegin", listFolders());
let folders = JSON.parse(GM_getValue("folders"));
for (let i = 0; i < folders.folders.length; i++){
document.getElementsByClassName("plus-add-folder-" + i)[0].addEventListener("click", function(){addEntry(i)});
}
}
document.getElementsByClassName("plus-folder-trigger")[0].addEventListener("click", reloadFolders);
}
function markAs(){
let mark_button = '
' +
'' +
'
' +
'
' +
' Read
' +
'
' +
' Unread
' +
'
' +
'
 ';
let position2 = document.getElementsByClassName("plus-actionbar")[0];
position2.insertAdjacentHTML("beforeend", mark_button);
}
function startReading(){
let manga_id = getSubPage(2);
let chapter_id = "";
let api_url = "https://mangadex.org/api/manga/" + manga_id;
async function getapi(url) {
let response = await fetch(url);
var data = await response.json();
var data2 = "";
for(let i = 0; i < Object.entries(data.chapter).length; i++){
if (await Object.entries(Object.entries(data.chapter)[i][1])[3][1] == "English"){
data2 = await Object.entries(data.chapter)[i][0];
break;
}
}
data.chapter[data2]
return data2
}
getapi(api_url).then(x => {
chapter_id = x;
let link = "https://mangadex.org/chapter/" + chapter_id;
let mark_button = ''
var position2 = document.getElementsByClassName("plus-actionbar")[0];
position2.insertAdjacentHTML("beforeend", mark_button);
});
}
makeBar();
addToFolder();
markAs();
startReading();
}
function preload(){
async function doPreload(){
let index = 100;
for (let i = 0; i < index; i++){
if (document.getElementById("preload-all").getAttribute("disabled") == ""){
await new Promise(r => setTimeout(r, 100));
}
else{
document.getElementById("preload-all").click();
i = index;
}
}
}
let enabled = (JSON.parse(GM_getValue("options"))).options[0].preloading
if (enabled == 0){
doPreload();
}
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '37' || e.keyCode == '39') {
doPreload();
}
}
document.onkeydown = checkKey;
document.getElementsByClassName("reader-images col-auto row no-gutters flex-nowrap m-auto text-center cursor-pointer directional constrained")[0].onclick = function(){doPreload()};
}
function resets(level){
if(level == "r"){
let options = {"options" : [{"preloading" : 0}]};
GM_setValue("options", JSON.stringify(options));
location.reload();
}
else if(level == "w"){
let folders = {"folders" : []};
GM_setValue("folders", JSON.stringify(folders));
location.reload();
}
else if(level == "rw"){
let options = {"options" : [{"preloading" : 0}]};
GM_setValue("options", JSON.stringify(options));
let folders = {"folders" : []};
GM_setValue("folders", JSON.stringify(folders));
GM_deleteValue("first_time");
location.reload();
}
}
startup();
})();