// ==UserScript== // @name Blur Title Reddit // @namespace https://greasyfork.org/users/102866 // @description Blurring a title which marked as spoiler in reddit, just like in fallout subreddit. // @include https://*.reddit.com/* // @include http://*.reddit.com/* // @exclude https://*.reddit.com/r/fallout/* // @exclude http://*.reddit.com/r/fallout/* // @exclude https://*.reddit.com/r/*/comments/* // @exclude http://*.reddit.com/r/*/comments/* // @require https://code.jquery.com/jquery-3.1.1.min.js // @author TiLied // @version 0.5.00 // @grant GM_listValues // @grant GM_getValue // @grant GM_setValue // @grant GM_deleteValue // @grant GM_registerMenuCommand // @downloadURL none // ==/UserScript== // Append some text to the element with id someText using the jQuery library. TEST JQUERY //$("#hsts_pixel").append(" more text."); //not empty val var titles = document.querySelectorAll("a.title"), titlesDivO = document.querySelectorAll("div.spoiler"); //const const stringStartbdi = '', stringEndbdi = '', oneSecond = 1000; //empty val var res, currentLocation, string, stringCSS, firstB, lastB, lengthOfIndexes; //arrays var titlesDiv = [], titlesTitle = [], stringArr = [], stringOri = [], originStrings = [], len = [], arrBeg = [], arrEnd = []; //prefs var btr_pTitle, btr_pUsers, asterisk, debug; //tests //if (currentLocation === undefined) { // currentLocation = window.location; //} Main(); function Main() { console.log("Blur Title Reddit v" + GM_info.script.version + " Initialized"); //Place css in
SetCSS(); //Check settings or create them SetSettings(); //Menu Monkey Command GM_registerMenuCommand("Show Settings Blur Title Reddit", MenuCommand); //Track scroll for infinite reddit $(document).ready(function () { window.onscroll = function (ev) { if ((window.innerHeight + window.pageYOffset) >= document.getElementById("siteTable").scrollHeight) { UpdateDivs(); } }; CheckRES(); }); //Start function on bluring titles if (titlesDivO.length != 0) { console.log(titlesDivO); for (var i = 0; i < titlesDivO.length; i++) { titlesDiv[i] = titlesDivO[i]; } console.log(titlesDiv); MyFunction(); } //Set UI of settings OptionsUI(); } //set settings function SetSettings() { //Delete old values DeleteValues("old"); //THIS IS ABOUT DEBUG if (HasValue("btr_debug", false)) { debug = GM_getValue("btr_debug"); } //THIS IS ABOUT TITLE if (HasValue("btr_GMTitle", false)) { btr_pTitle = GM_getValue("btr_GMTitle"); } //THIS IS ABOUT asterisk if (HasValue("btr_asterisk", false)) { asterisk = GM_getValue("btr_asterisk"); } //THIS IS ABOUT USERS if (HasValue("btr_pUsers", "none")) { btr_pUsers = GM_getValue("btr_pUsers"); } //Console log prefs with value console.log("*prefs:"); console.log("*-----*"); var vals = []; for (var i = 0; i < GM_listValues().length; i++) { vals[i] = GM_listValues()[i]; } for (var i = 0; i < vals.length; i++) { console.log("*" + vals[i] + ":" + GM_getValue(vals[i])); } console.log("*-----*"); } //Check if value exists or not. optValue = Optional function HasValue(nameVal, optValue) { var vals = []; for (var i = 0; i < GM_listValues().length; i++) { vals[i] = GM_listValues()[i]; } if (vals.length === 0) { if (optValue != undefined) { GM_setValue(nameVal, optValue); return true; } else { return false; } } if (typeof nameVal != "string") { return alert("name of value: '" + nameVal + "' are not string"); } for (var i = 0; i < vals.length; i++) { if (vals[i] === nameVal) { return true; } } if (optValue != undefined) { GM_setValue(nameVal, optValue); return true; } else { return false; } } //Delete Values function DeleteValues(nameVal) { var vals = []; for (var i = 0; i < GM_listValues().length; i++) { vals[i] = GM_listValues()[i]; } if (vals.length === 0 || typeof nameVal != "string") { return; } switch(nameVal) { case "all": for (var i = 0; i < vals.length; i++) { GM_deleteValue(vals[i]); } break; case "old": for (var i = 0; i < vals.length; i++) { if (vals[i] === "debug" || vals[i] === "debugA") { GM_deleteValue(vals[i]); } } break; default: for (var i = 0; i < vals.length; i++) { if (vals[i] === nameVal) { GM_deleteValue(nameVal); } } break; } } //css function SetCSS() { $("head").append($("")); $("head").append($("").text("bdi.btr_title:hover \ { \ color:inherit!important; \ background:transparent!important; \ text-decoration:none!important; \ text-shadow:0 0.1px 0 #dcddce \ } \ ")); $("head").append($("").text(" bdi.btr_title { \ color:rgba(255,60,231,0) !important; \ text-shadow: 0px 0px 1em black; \ padding: 0 2px; \ } \ ")); $("head").append($("").text(" \ bdi.btr_trans \ { \ transition: all 0.5s ease; \ } \ ")); $("head").append($("").text(" \ .btr_closeButton \ { \ cursor: pointer; \ text-align: center; \ font-size: 11px; \ float:right; \ margin-top:0px; \ border:1px solid #AAA; \ width:16px; \ height:16px; \ } \ ")); $("head").append($("").text(" \ .btr_closeButton:hover \ { \ border:1px solid #999; \ background-color: #ddd; \ } \ ")); $("head").append($("").text(" \ .title \ { \ overflow: visible !important; \ } \ ")); $("head").append($("").text("div.btr_opt { \ position: fixed; bottom: 0; right: 0; border: 0; \ }")); $("head").append($("")); } //Check Reddit Enhancement Suite function CheckRES() { if (debug) { console.log("CHECK RES/NER:"); console.group(); } if ($(".neverEndingReddit").length === 0) { if (debug) { console.log($(".neverEndingReddit")); } if ($(".neverEndingReddit").length === 0) { if (debug) { console.log($(".neverEndingReddit")); } if ($(".neverEndingReddit").length === 0) { if (debug) { console.log($(".neverEndingReddit")); } if ($(".neverEndingReddit").length === 0) { setTimeout(function () { if (debug) { console.log($(".neverEndingReddit")); } if ($(".neverEndingReddit").length === 0) { setTimeout(function () { if (debug) { console.log($(".neverEndingReddit")); console.groupEnd(); } if ($(".neverEndingReddit").length === 0) { res = false; return; } else { SearchForNER(); return; } }, 4500); } else { SearchForNER(); return; } }, 2000); } else { SearchForNER(); return; } } else { SearchForNER(); return; } } else { SearchForNER(); return; } //console.log("Check current RES : " + res[0]); } else { SearchForNER(); if (debug) { console.groupEnd(); } return; } //console.log("Check current RES : " + res[0]); } function SearchForNER() { res = true; if (debug) { console.log("Check current RES : " + res); } $(".neverEndingReddit").on("click", function () { //alert("Settings has been changed. Now brackets hiding."); TimeOut(oneSecond, function () { UpdateDivs(); CheckRES(); }); }); } //DO NOT ASK ME WHY function TimeOut(baseNumber, func) { setTimeout(func, baseNumber); } //UI FOR SETTINGS function OptionsUI() { const settingsDiv = $("").html("show settings blur title reddit
"); //} else { //$(".side").append(""); //} $(".side:first").append(""); $(document).ready(function () { $("#btr_hide").click(function () { $("#btrSettings").hide(1000); }); $(".btr_closeButton").click(function () { $("#btrSettings").hide(1000); }); $("#btr_show").click(function () { $("#btrSettings").show(1000); $("#btrSettings").addClass("btr_opt"); }); }); } function UpdateDivs() { var oldLength = titlesDivO.length; titlesDivO = document.querySelectorAll("div.spoiler"); if (titlesDivO.length > oldLength) { console.log(titlesDivO); for (var i = 0; i < titlesDivO.length; i++) { titlesDiv[i] = titlesDivO[i]; } console.log(titlesDiv); for (var i = 0; i < titlesDiv.length; i++) { if (titlesDiv[i].querySelector("div.entry.unvoted")) { titlesTitle[i] = titlesDiv[i].querySelector("div.entry.unvoted").querySelector("p.title").querySelector("a.title"); if (i >= oldLength) { originStrings.push(titlesTitle[i].innerHTML); // originStrings[i] = titlesTitle[i].innerHTML; } } else { titlesTitle[i] = titlesDiv[i].querySelector("div.entry").querySelector("p.title").querySelector("a.title"); if (i >= oldLength) { originStrings.push(titlesTitle[i].innerHTML); // originStrings[i] = titlesTitle[i].innerHTML; } } } if (debug) { console.log(originStrings); } oldLength = titlesDivO.length; MyFunction(); } else { if (res) { TimeOut(oneSecond, function () { UpdateDivs(); CheckRES(); }); } else { return; } } } function MyFunction() { if (titlesTitle.length === 0) { for (var i = 0; i < titlesDiv.length; i++) { if (titlesDiv[i].querySelector("div.entry.unvoted")) { titlesTitle[i] = titlesDiv[i].querySelector("div.entry.unvoted").querySelector("p.title").querySelector("a.title"); originStrings[i] = titlesTitle[i].innerHTML; } else { titlesTitle[i] = titlesDiv[i].querySelector("div.entry").querySelector("p.title").querySelector("a.title"); originStrings[i] = titlesTitle[i].innerHTML; } } } for (var i = 0; i < titlesDiv.length; i++) { len[i] = titlesTitle[i].innerHTML.length; if (debug) { console.log(titlesTitle[i].innerHTML, len[i]); } stringArr[i] = titlesTitle[i].innerHTML; if (stringArr[i].toString().search(stringStartbdi)) { stringArr[i] = originStrings[i]; } FindBracPref(len[i], stringArr[i], titlesTitle[i]); //console.log("array " + stringArr[i]); } } function FindBracPref(l, sArr, tTitle) { //console.log(GM_getValue("btr_GMTitle") + ' and btr_pTitle: ' + btr_pTitle); if (btr_pTitle === true) { // console.log(GetAllIndexes(sArr, "[", "(")); lengthOfIndexes = 0; FindBrac(l, sArr, tTitle, lengthOfIndexes); } else if (asterisk === true) { lengthOfIndexes = GetAllIndexes(sArr, "[", "(").length; lengthOfIndexes += GetAllIndexes(sArr, "*", "even").length; FindBrac(l, sArr, tTitle, lengthOfIndexes); } else { lengthOfIndexes = GetAllIndexes(sArr, "[", "(").length; FindBrac(l, sArr, tTitle, lengthOfIndexes); } } function FindBrac(l, sArr, tTitle, lengthOfIndexes) { switch(lengthOfIndexes) { case 0: ChangeString(l, sArr, tTitle, 0); break; case 1: ChangeString(l, sArr, tTitle, 1); break; case 2: ChangeString(l, sArr, tTitle, 2); break; case 3: ChangeString(l, sArr, tTitle, 3); break; case 4: ChangeString(l, sArr, tTitle, 1); //TODO 4 break; case 5: ChangeString(l, sArr, tTitle, 1); //TODO MAYBE 5 break; default: ChangeString(l, sArr, tTitle, 0); return; } } function ChangeString(l, sArr, tTitle, amount) { arrBeg = GetAllIndexes(sArr, "[", "("); arrEnd = GetAllIndexes(sArr, "]", ")"); if (asterisk === true) { arrBeg = arrBeg.concat(GetAllIndexes(sArr, "*", "even")); arrEnd = arrEnd.concat(GetAllIndexes(sArr, "*", "odd")); } if (debug) { console.log("*str of brackets :", arrBeg); console.log("*end of brackets :", arrEnd); } if (amount === 0) { string = stringStartbdi + ' ' + sArr + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } if (amount === 1) { if (debug) { console.log("*words in brackets :", sArr.substring(arrBeg[0], arrEnd[0] + 1)); } //IF WHOLE TITLE IN BRACKETS if (arrBeg[0] <= 2 && arrEnd[0] >= l - 2) { string = stringStartbdi + ' ' + sArr + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } if (arrBeg[0] <= 2) { string = sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, l) + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else if (arrEnd[0] >= l - 2) { string = stringStartbdi + ' ' + sArr.substring(0, arrBeg[0]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[0], l); if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else { string = stringStartbdi + ' ' + sArr.substring(0, arrBeg[0]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, l) + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } } if (amount === 2) { var a; var s = ''; for (a = 0; a < arrBeg.length; a++) { s += sArr.substring(arrBeg[a], arrEnd[a] + 1) + ' '; } if (debug) { console.log("*words in brackets :", s); } //IF WHOLE TITLE IN BRACKETS, NOT WORKING CORRECTLY TODO! if ((arrBeg[0] <= 2 && arrEnd[0] >= l - 2) || (arrBeg[1] <= 2 && arrEnd[1] >= l - 2)) { string = stringStartbdi + ' ' + sArr + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } if (arrBeg[0] <= 2) { if (arrEnd[0] + 4 > arrBeg[1]) { string = sArr.substring(arrBeg[0], arrEnd[1] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[1] + 1, l) + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else if (arrEnd[1] >= l - 2) { string = sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, arrBeg[1]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[1], l); if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else { string = sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, arrBeg[1]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[1], arrEnd[1] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[1] + 1, l) + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } } else if (arrEnd[1] >= l - 2) { if (arrBeg[1] - 4 < arrEnd[0]) { string = stringStartbdi + ' ' + sArr.substring(0, arrBeg[0]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[0], l); if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else if (arrBeg[0] <= 2) { string = sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, arrBeg[1]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[1], l); if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else { string = stringStartbdi + ' ' + sArr.substring(0, arrBeg[0]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, arrBeg[1]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[1], l); if (debug) { console.info(string); } tTitle.innerHTML = string; return; } } else { if (arrEnd[0] + 3 >= arrBeg[1]) { string = stringStartbdi + ' ' + sArr.substring(0, arrBeg[0]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[0], arrEnd[1] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[1] + 1, l) + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } else { string = stringStartbdi + ' ' + sArr.substring(0, arrBeg[0]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[0], arrEnd[0] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[0] + 1, arrBeg[1]) + ' ' + stringEndbdi + ' ' + sArr.substring(arrBeg[1], arrEnd[1] + 1) + ' ' + stringStartbdi + ' ' + sArr.substring(arrEnd[1] + 1, l) + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } } } //Three groups of brackets //example sentence: "[spoiler0]_text1_[spoiler1]_text2_[spoiler2]" if (amount === 3) { var a; var s = ''; for (a = 0; a < arrBeg.length; a++) { s += sArr.substring(arrBeg[a], arrEnd[a] + 1) + ' '; } if (debug) { console.log("*words in brackets :", s); } //IF WHOLE TITLE IN BRACKETS, NOT WORKING CORRECTLY TODO! if ((arrBeg[0] <= 2 && arrEnd[0] >= l - 2) || (arrBeg[1] <= 2 && arrEnd[1] >= l - 2) || (arrBeg[2] <= 2 && arrEnd[2] >= l - 2)) { string = stringStartbdi + ' ' + sArr + ' ' + stringEndbdi; if (debug) { console.info(string); } tTitle.innerHTML = string; return; } //case one:"[spoiler0]..." if (arrBeg[0] <= 2) { //case one:one:"[spoiler0][spoiler1]..." if (arrEnd[0] + 4 > arrBeg[1]) { //case one:one:one:"[spoiler0][spoiler1][spoiler2]_text" if (arrEnd[1] + 4 > arrBeg[2]) { //"[spoiler0][spoiler1][spoiler2]