Akira / POPO Obfuscator

// ==UserScript==
// @name POPO Obfuscator
// @description POPO章回內文混淆器
// @version 1.10
// @author Akira
// @match *://www.popo.tw/panel/literature/*
// @run-at document-end
// @grant none
// @license MIT
// @updateURL https://openuserjs.org/meta/Akira/POPO_Obfuscator.meta.js
// @downloadURL https://openuserjs.org/src/scripts/Akira/POPO_Obfuscator.user.js
// ==/UserScript==

(function(){
	"use strict";
	
	var _CHPattern = new RegExp("[\ufe30-\uffe6\u2010-\u203b\u3000-\u3029\u4e00-\u9fa5]", "g");
	var _SPPattern = new RegExp("[\u3000\u200b\u200c\u200d\x20\r\n]", "g");
	var _OBPattern = new RegExp("[\u200b\u200c\u200d\u2060]", "g");
	function obfuscator(w){return "\u200b\u200d\u200c"+w;}
	function count(text){
		var realLength = text.replace(_SPPattern, "").length;
		var _OBLength = text.length-text.replace(_OBPattern, "").length;
		var $btn = document.getElementById("poplus_purge");
		if($btn){
			$btn.innerHTML = "移除混淆碼" + (_OBLength ? "(目前 " + _OBLength + " 字)" : "");
			$btn.className = _OBLength ? "btn-default btn-accept" : "btn-default";
		}
		return realLength;
	}
	
	var $field, $buttons;
	var $title = document.getElementById("Articles_title");
	var $content = document.getElementById("ArticleTexts_content");
	if($title && $content){
		$field = document.createElement("div");
		$field.className = "field";
		$field.innerHTML = '<div class="label"><label>POPlus</label></div><div class="setting"><button class="btn-default btn-accept">排版</button> <button class="btn-default btn-accept">混淆</button> <button id="poplus_purge" class="btn-default">移除混淆碼</button></div>';
		$content.parentNode.parentNode.parentNode.insertBefore($field, $content.parentNode.parentNode);
		
		$buttons = $field.querySelectorAll("button");
		
		$buttons[0].addEventListener("click", function (e){
			var src = $content.value.split("\n");
			var dest = [];
			for(var i in src){
				var p = src[i].trim();
				p.length && dest.push("\u2003\u2003" + p);
			}
			$content.value = dest.join("\n\n");
			count_words();
			e.preventDefault();
		});
		
		$buttons[1].addEventListener("click", function action_b(e){
			var text = $title.value.replace(_OBPattern, "");
			var i;
			for(i=2+Math.floor(Math.random()*3);i>0;i--){
				text = text.replace(_CHPattern, obfuscator);
			}
			for(i=8+Math.floor(Math.random()*8);i>0;i--){
				text = obfuscator(text);
			}
			$title.value = text;
			$content.value = $content.value.replace(_CHPattern, obfuscator);
			count_words();
			e.preventDefault();
		});
		
		$buttons[2].addEventListener("click", function action_c(e){
			$title.value = $title.value.replace(_OBPattern, "");
			$content.value = $content.value.replace(_OBPattern, "");
			count_words();
			e.preventDefault();
		});
		
		var $words = document.getElementById("words") || {};
		self.count_now = function(text){
			var n = count(text);
			$words.value = n;
			return n;
		};
		
		self.count_words = function(){
			return count_now($content.value);
		};
		
		count_words();
	}
	
	var $bookName = document.getElementById("Books_name");
	if($bookName){
		$field = document.createElement("div");
		$field.className = "field";
		$field.innerHTML = '<div class="label"><label>POPlus</label></div><div class="setting"><button class="btn-default btn-accept">書名混淆</button> <button id="poplus_purge" class="btn-default">移除混淆碼</button></div>';
		$bookName.parentNode.parentNode.parentNode.insertBefore($field, $bookName.parentNode.parentNode);
		
		$buttons = $field.querySelectorAll("button");
		
		$buttons[0].addEventListener("click", function (e){
			var text = $bookName.value.replace(_OBPattern, "");
			var i;
			for(i=2+Math.floor(Math.random()*3);i>0;i--){
				text = text.replace(_CHPattern, obfuscator);
			}
			for(i=8+Math.floor(Math.random()*8);i>0;i--){
				text = obfuscator(text);
			}
			$bookName.value = text;
			count(text);
			e.preventDefault();
		});
		
		$buttons[1].addEventListener("click", function (e){
			$bookName.value = $bookName.value.replace(_OBPattern, "");
			count($bookName.value);
			e.preventDefault();
		});
		
		$bookName.addEventListener("keyup", function(){
			count($bookName.value);
		});
		
		count($bookName.value);
	}
}());