// ==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/*/comments/*
// @exclude http://*.reddit.com/r/*/comments/*
// @author TiLied
// @version 0.8.00
// @grant GM_listValues
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// @grant GM.listValues
// @grant GM.getValue
// @grant GM.setValue
// @grant GM.deleteValue
// @downloadURL https://update.greasyfork.icu/scripts/27404/Blur%20Title%20Reddit.user.js
// @updateURL https://update.greasyfork.icu/scripts/27404/Blur%20Title%20Reddit.meta.js
// ==/UserScript==
class BlurTitleReddit
{
constructor()
{
console.log("Blur Title Reddit v" + GM.info.script.version + " initialization");
this.Debug = false;
this.btr_pTitle = false;
this.asterisk = true;
this.Scroll = 1;
this._FirstTime();
this._SetCSS();
}
_SetCSS()
{
document.head.append("");
document.head.insertAdjacentHTML("beforeend", ``);
document.head.insertAdjacentHTML("beforeend", ``);
document.head.insertAdjacentHTML("beforeend", ``);
document.head.insertAdjacentHTML("beforeend", ``);
document.head.insertAdjacentHTML("beforeend", ``);
document.head.insertAdjacentHTML("beforeend", ``);
document.head.insertAdjacentHTML("beforeend", ``);
document.head.append("");
}
async _FirstTime()
{
if (this.HasValueGM("btr_GMTitle", false))
{
this.btr_pTitle = await GM.getValue("btr_GMTitle");
}
if (this.HasValueGM("btr_asterisk", true))
{
this.asterisk = await GM.getValue("btr_asterisk");
}
//Console log prefs with value
console.log("*prefs:");
console.log("*-----*");
let vals = await GM.listValues();
//Find out that var in for block is not local... Seriously js?
for (let i = 0; i < vals.length; i++)
{
console.log("*" + vals[i] + ":" + await GM.getValue(vals[i]));
}
console.log("*-----*");
}
Main()
{
window.onscroll = function (ev)
{
if (window.pageYOffset >= window.innerHeight * btr.Scroll)
{
btr.Core();
btr.Scroll += 1;
}
};
this.Core();
//Set UI of settings
//TODO UI!
//this.OptionsUI();
}
Core()
{
let _titlesDivQ = document.querySelectorAll("div.spoiler");
let _titlesDiv = [];
if (_titlesDivQ.length == 0)
{
function _IsSpoiler(_el)
{
let _spans = _el.querySelectorAll("span");
if (btr.Debug)
console.log(_el.querySelectorAll("span"));
for (let i = 0; i <= _spans.length; i++)
{
if (i == _spans.length)
return false;
if (_spans[i].textContent == 'spoiler')
return true;
}
}
let _titles = document.querySelectorAll(".Post");
for (let i = 0; i < _titles.length; i++)
{
if (_IsSpoiler(_titles[i]))
{
_titlesDiv.push(_titles[i]);
}
}
} else
{
for (let i = 0; i < _titlesDivQ.length; i++)
{
_titlesDiv.push(_titlesDivQ[i]);
}
}
if (_titlesDiv != this.titlesDiv)
{
if (this.Debug)
console.log(_titlesDiv);
this.titlesDiv = _titlesDiv;
for (let i = 0; i < _titlesDiv.length; i++)
{
if (_titlesDiv[i].querySelectorAll(".btr_title").length > 0)
continue;
let _title = _titlesDiv[i].querySelectorAll("h3, a.title");
if (this.Debug)
console.log(_title);
if (this.btr_pTitle == true)
{
let lengthOfIndexes = 0;
this.ChangeString(_title[0].textContent.length, _title[0].textContent, _title[0], lengthOfIndexes);
}
else if (this.asterisk == true)
{
let lengthOfIndexes = this.GetAllIndexes(_title[0].textContent, "[", "(").length;
lengthOfIndexes += this.GetAllIndexes(_title[0].textContent, "*", "even").length;
this.ChangeString(_title[0].textContent.length, _title[0].textContent, _title[0], lengthOfIndexes);
} else
{
let lengthOfIndexes = this.GetAllIndexes(_title[0].textContent, "[", "(").length;
this.ChangeString(_title[0].textContent.length, _title[0].textContent, _title[0], lengthOfIndexes);
}
}
}
}
ChangeString(l, sArr, tTitle, amountL)
{
const stringStartbdi = '',
stringEndbdi = '';
let amount = amountL;
let string = "";
if (amount > 3)
amount = 1;
let arrBeg = this.GetAllIndexes(sArr, "[", "(");
let arrEnd = this.GetAllIndexes(sArr, "]", ")");
if (this.asterisk === true)
{
arrBeg = arrBeg.concat(this.GetAllIndexes(sArr, "*", "even"));
arrEnd = arrEnd.concat(this.GetAllIndexes(sArr, "*", "odd"));
}
arrBeg.sort(function (a, b)
{
return a - b;
});
arrEnd.sort(function (a, b)
{
return a - b;
});
if (amount === 0)
{
string = stringStartbdi + ' ' + sArr + ' ' + stringEndbdi;
if (this.Debug)
{
console.info(string);
}
tTitle.innerHTML = string;
return;
}
if (amount === 1)
{
if (this.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 (this.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 (this.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 (this.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 (this.Debug)
{
console.info(string);
}
tTitle.innerHTML = string;
return;
}
}
if (amount === 2)
{
let s = '';
for (let a = 0; a < arrBeg.length; a++)
{
s += sArr.substring(arrBeg[a], arrEnd[a] + 1) + ' ';
}
if (this.Debug)
{
console.log("*words in brackets :", s);
}
//IF TITLE HAS ONE BRACKET WITHOUT CLOSING ONE
if (arrBeg.length !== arrEnd.length)
{
ChangeString(l, sArr, tTitle, 1);
return;
}
//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 (this.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 (this.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 (this.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 (this.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 (this.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 (this.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 (this.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 (this.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 (this.Debug)
{
console.info(string);
}
tTitle.innerHTML = string;
return;
}
}
}
//Three groups of brackets
//example sentence: "[spoiler0]_text1_[spoiler1]_text2_[spoiler2]"
if (amount === 3)
{
let s = '';
for (let a = 0; a < arrBeg.length; a++)
{
s += sArr.substring(arrBeg[a], arrEnd[a] + 1) + ' ';
}
if (this.Debug)
{
console.log("*words in brackets :", s);
}
//IF TITLE HAS ONE BRACKET WITHOUT CLOSING ONE
if (arrBeg.length !== arrEnd.length)
{
ChangeString(l, sArr, tTitle, 1);
return;
}
//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 (this.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]