Greasy Fork

Manganelo Helper

Allows you to use the left and right arrow keys to navigate chapters at Manganelo, and removes space between images.

目前为 2020-10-09 提交的版本。查看 最新版本

// ==UserScript==
// @name 			Manganelo Helper
// @namespace 		GF-Fear3d
// @version 		0.5
// @description 	Allows you to use the left and right arrow keys to navigate chapters at Manganelo, and removes space between images.
// @author 			Fear3d
// @match 			https://manganelo.com/*
// @require 		https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js
// @grant			GM_registerMenuCommand
// @grant			GM_setValue
// @grant			GM_getValue
// ==/UserScript==

// ****Attention**** This script is not compatible with Greasemonkey 4.0+. Please use Tampermonkey instead.

(function() {
 	'use strict';

 	var nextUrl = "";
 	var prevUrl = "";
 	var currentTitle = "";
 	var uiInitialized = false;
 	var atChapter = false;
 	var atManga = false;

 	// ************************************************************************************************************************************
 	// ************ As of version 0.2 you should not add your new titles here.                             ********************************
 	// ************ You should now add/remove titles through the configuration menu.                       ********************************
 	// ************ Visit https://greasyfork.org/en/scripts/412420-manganelo-helper if you don't know how. ********************************
 	// ************ If you add them via the menu, your titles will be saved even when I update the script. ********************************
 	// ************************************************************************************************************************************
 	// Default list of titles that we want to have no image margines
 	var titleListDefault = [
 		"A Returner's Magic Should Be Special",
 		"Solo Leveling",
 		"I Am The Sorcerer King",
 		"I, Who Blocked The Demon King's Ultimate Attack, Ended Up As The Little Hero's Nanny!",
 		"The Top Clan Leader In History",
 		"The Ghostly Doctor",
 		"God Of Martial Arts",
 		"Spirit Sword Sovereign",
 		"Tang Yin Zai Yi Jie",
 		"The God Of High School",
 		"Tower Of God",
 		"Tomb Raider King"
 	];

 	var titleList;

 	// Configuration UI elements
 	var configWindow = document.createElement("div");
 	var lblTitleList = document.createElement("label");
 	var taTitleList = document.createElement("textarea");
 	var btnAddTitle = document.createElement("button");
 	var btnSave = document.createElement("button");
 	var btnCancel = document.createElement("button");
 	var btnDefault = document.createElement("button");
 	var divButtons = document.createElement("div");

 	function btnAddTitleClick()
 	{
 		if (atChapter)
 			taTitleList.value = taTitleList.value + "\r\n" + currentTitle;
 		else if (atManga)
 			taTitleList.value = taTitleList.value + "\r\n" + $(".panel-breadcrumb > a:nth-child(3)").text().trim();
 	}

 	function btnSaveClick()
 	{
 		titleList = taTitleList.value.split(/\r?\n/);
 		GM_setValue("titleList", JSON.stringify(titleList));
 		configWindow.style.display = "none";

 		if (atChapter)
 			removeMargines();
 	}

 	function btnCancelClick()
 	{
 		configWindow.style.display = "none";
 	}

 	function btnDefaultClick()
 	{
 		taTitleList.value = titleListDefault.join("\r\n");
 	}

 	// Initializes configuration UI elements
 	function initConfigUI()
 	{
		lblTitleList.innerHTML = "Titles to remove gaps from: ";
		lblTitleList.setAttribute("for", "taTitleList");
		configWindow.appendChild(lblTitleList);
		configWindow.appendChild(document.createElement("br"));

		taTitleList.value = titleList.join("\r\n");
		taTitleList.setAttribute("id", "taTitleList");
		taTitleList.setAttribute("rows", "30");
		taTitleList.setAttribute("cols", "80");
		taTitleList.style.whiteSpace = "pre";
		taTitleList.style.width = "670px";
		configWindow.appendChild(taTitleList);

		btnDefault.innerHTML = "Restore Default";
		btnDefault.style.margin = "5px";
		btnDefault.style.marginLeft = "10px";
		btnDefault.style.marginBottom = "10px";
		btnDefault.style.fontSize = "18px";
		btnDefault.style.cssFloat = "left";
		btnDefault.onclick = function() { btnDefaultClick() };
		divButtons.appendChild(btnDefault);

		btnAddTitle.innerHTML = "Add Current Title";
		btnAddTitle.style.margin = "5px";
		btnAddTitle.style.marginBottom = "10px";
		btnAddTitle.style.fontSize = "18px";
		btnAddTitle.onclick = function() { btnAddTitleClick() };
		if (atChapter || atManga)
			btnAddTitle.disabled = false;
		else
			btnAddTitle.disabled = true;
		divButtons.appendChild(btnAddTitle);

		btnCancel.innerHTML = "Cancel";
		btnCancel.style.margin = "5px";
		btnCancel.style.marginRight = "10px";
		btnCancel.style.marginBottom = "10px";
		btnCancel.style.fontSize = "18px";
		btnCancel.style.cssFloat = "right";
		btnCancel.onclick = function() { btnCancelClick() };
		divButtons.appendChild(btnCancel);

		btnSave.innerHTML = "Save";
		btnSave.style.margin = "5px";
		btnSave.style.marginBottom = "10px";
		btnSave.style.fontSize = "18px";
		btnSave.style.cssFloat = "right";
		btnSave.onclick = function() { btnSaveClick() };
		divButtons.appendChild(btnSave);

		divButtons.style.textAlign = "center";
		configWindow.appendChild(divButtons);

		configWindow.style.position = "fixed";
		configWindow.style.top = "50%";
		configWindow.style.left = "50%";
		configWindow.style.textAlign = "center";
		configWindow.style.width = "700px";
		configWindow.style.transform = "translate(-50%, -50%)";
		configWindow.style.backgroundColor = "darkviolet";
		configWindow.style.color = "whitesmoke";
		document.body.appendChild(configWindow);

		uiInitialized = true;
	}

 	 // Displays configuration UI
 	function openConfig()
 	{
 		if (!uiInitialized) {
 			initConfigUI();
 		}
 		else {
 			taTitleList.value = titleList.join("\r\n");
 			configWindow.style.display = "block";
 		}
 	}


 	// Remove margins between images on specified titles
 	function removeMargines() 
 	{
 		if (titleList.indexOf(currentTitle) == -1)
 			return;

 		$(".container-chapter-reader img").css("margin", "0px auto 0");
 	}

 	// Find URLs for Next and Prev
 	function findUrls()
 	{
 		var nextPage, prevPage;

 		currentTitle = $("div.container:nth-child(1) > div.panel-breadcrumb:nth-child(2) > a:nth-child(3)").text();

 		if ($("a.navi-change-chapter-btn-next.a-h").length) {
 			nextPage = $("a.navi-change-chapter-btn-next.a-h:first");
 		} 
 		else {
 			nextPage = $("div.container:nth-child(1) > div.panel-breadcrumb:nth-child(2) > a:nth-child(3)");
 		}

 		if ($("a.navi-change-chapter-btn-prev.a-h").length) {
 			prevPage = $("a.navi-change-chapter-btn-prev.a-h:first");
 		} 
 		else {
 			prevPage = $("div.container:nth-child(1) > div.panel-breadcrumb:nth-child(2) > a:nth-child(3)");
 		}

    	nextUrl = nextPage.attr("href");
    	prevUrl = prevPage.attr("href");
 	}

 	// Checks if this is the first run, and displays a message if so
 	function checkFirstRun()
 	{
 		var firstRun = GM_getValue("firstRun", true);

 		if (firstRun)
 		{
 			GM_setValue("firstRun", false);
 			alert("Welcome to Manganelo Helper 0.5\n\n" + "If you are upgrading from version 0.1, any titles that you had added to the script have " + 
 				"been lost. I sincerely apologize for this. I had originally written this script for my own personal use, so I didn't consider how " + 
 				"the old hardcoded title list would not be persistent (for other people) through updates.\n\nThe good news is that I have both fixed " +
 				"this issue, and also made it easier to add/remove titles. Now you just click on the Tampermonkey icon on your toolbar, click on " + 
 				"'Configure Titles', edit the list as you please, and then click 'Save'. Just make sure that each title is on a new line, and " + 
 				"remember that the titles are case sensetive.");
 		}
 	}

 	// Loads title list from storage if it exists, otherwise it uses the default
 	function loadTitleList()
 	{
 		var jTitleList = GM_getValue("titleList");

 		if (!jTitleList)
 			titleList = titleListDefault;
 		else
 			titleList = JSON.parse(jTitleList);
 	}

 	// Handle arrow key events
 	function attachArrowKeyEvent()
 	{
 		document.onkeydown = function(evt) {
			switch (evt.keyCode) {
				case 37: // Left Arrow
					window.location = prevUrl;
					break;
				case 39: // Right Arrow
					window.location = nextUrl;
					break;
			}
		};
 	}

 	$(document).ready(function() 
 	{
 		atChapter = /https:\/\/manganelo.com\/chapter\/\S+\/\S+/.test(window.location.href);
 		atManga = /https:\/\/manganelo.com\/manga\/[A-Za-z0-9\.\-~_%]+/.test(window.location.href);

 		if (atChapter)
 			findUrls();

 		loadTitleList();

 		if (atChapter) {
 			removeMargines();
 			attachArrowKeyEvent();
 		}

 		GM_registerMenuCommand("Configure Titles", function () { openConfig() }, "t");
 		checkFirstRun();
	});

})();