Created
May 26, 2016 01:34
-
-
Save anonymous/ecf2f737c98cf722502addc62afbee68 to your computer and use it in GitHub Desktop.
Plonking userscript for Linuxfr.org v3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name DLFPlonk | |
// @namespace DLFPlonk | |
// @include https://linuxfr.org/* | |
// @version 3 | |
// @grant GM_xmlhttpRequest | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM_deleteValue | |
// @grant GM_addStyle | |
// ==/UserScript== | |
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
// | |
// 0. You just DO WHAT THE FUCK YOU WANT TO. | |
// | |
(function() { | |
let plonkList = GM_getValue("plonkList","").split(","); | |
if(plonkList.length === 1 && plonkList[0] === ""){ | |
plonkList = []; | |
} | |
let plonkClosedAccounts = GM_getValue("plonkClosedAccounts",true); | |
GM_addStyle(".overlaybg{position:fixed;top:0;left:0;width: 100%;height: 100%;background-color:#000; opacity:0.5;z-index:100}"); | |
GM_addStyle(".overlaywin{position:fixed;left:0;right:0;top:0;bottom:0;width:600px;height:300px;margin:auto;background-color:#FFF;z-index:101;padding:10px 20px;border:thick double #000}"); | |
GM_addStyle(".overlaywin select{width:100%;height:180px}"); | |
GM_addStyle(".overlaywin button{background-image:none;padding-left:5px}"); | |
GM_addStyle(".overlaywin button[disabled='disabled']{background-image:none;padding-left:5px;color:gray}"); | |
function closePlonkList(){ | |
document.getElementsByClassName("overlaywin").item(0).remove(); | |
document.getElementsByClassName("overlaybg").item(0).remove(); | |
location.reload(); | |
} | |
function buildForm(form,user){ | |
let p = document.createElement("p"); | |
let select = document.createElement("select"); | |
if(plonkList.length){ | |
p.appendChild(document.createTextNode("Liste des utilisateurs plonkés:")); | |
select.setAttribute("id","plonkList"); | |
select.setAttribute("multiple","multiple"); | |
for (let i = 0; i < plonkList.length; i++){ | |
let option = document.createElement("option"); | |
option.setAttribute("value",plonkList[i]); | |
option.appendChild(document.createTextNode(plonkList[i])); | |
select.appendChild(option); | |
} | |
}else{ | |
p.appendChild(document.createTextNode("Aucun utilisateur plonké pour le moment.")); | |
select.remove(); | |
select = null; | |
} | |
form.appendChild(p); | |
if(select){ | |
form.appendChild(select); | |
} | |
let checkbox = document.createElement("input"); | |
checkbox.setAttribute("type","checkbox"); | |
checkbox.setAttribute("id","closedAccounts"); | |
checkbox.addEventListener("change",function(){plonkClosedAccounts=!plonkClosedAccounts; GM_setValue("plonkClosedAccounts",plonkClosedAccounts);}); | |
if(plonkClosedAccounts){ | |
checkbox.setAttribute("checked","checked"); | |
} | |
let label = document.createElement("label"); | |
label.appendChild(checkbox); | |
label.appendChild(document.createTextNode("plonker les comptes fermés.")); | |
let div = document.createElement("div"); | |
div.appendChild(label); | |
form.appendChild(div); | |
let addButton = document.createElement("button"); | |
addButton.setAttribute("id","plonkAdd"); | |
addButton.appendChild(document.createTextNode("Plonker '"+user+"'")); | |
addButton.addEventListener("click",function(){plonk(plonkList,user);},true); | |
if(plonkList.indexOf(user) !== -1){ | |
addButton.setAttribute("disabled","disabled"); | |
} | |
form.appendChild(addButton); | |
let delButton = document.createElement("button"); | |
delButton.appendChild(document.createTextNode("Supprimer la sélection")); | |
delButton.addEventListener("click",function(){unplonk(plonkList,user);},true); | |
if(!plonkList.length){ | |
delButton.setAttribute("disabled","disabled"); | |
} | |
form.appendChild(delButton); | |
let cancelButton = document.createElement("button"); | |
cancelButton.appendChild(document.createTextNode("Valider")); | |
cancelButton.addEventListener("click",function(){closePlonkList();},true); | |
form.appendChild(cancelButton); | |
} | |
function plonk(plonkList,user){ | |
plonkList.push(user); | |
GM_setValue("plonkList",plonkList.join(",")); | |
let select = document.getElementById("plonkList"); | |
if(!select){ | |
let form = document.getElementById("plonkForm"); | |
form.remove(); | |
form = document.createElement("form"); | |
form.setAttribute("id","plonkForm"); | |
buildForm(form,user); | |
document.getElementsByClassName("overlaywin").item(0).appendChild(form); | |
}else{ | |
let option = document.createElement("option"); | |
option.setAttribute("value",user); | |
option.appendChild(document.createTextNode(user)); | |
option.setAttribute("selected","selected"); | |
select.appendChild(option); | |
} | |
} | |
function unplonk(plonkList,user){ | |
select = document.getElementById("plonkList"); | |
for (let i = 0; i < select.length; i++){ | |
if(!select[i].selected){ | |
continue; | |
} | |
plonkList.splice(plonkList.indexOf(select[i]),1); | |
select[i].remove(); | |
} | |
if(plonkList.length === 1 && plonkList[0] === ""){ | |
plonkList=[]; | |
} | |
if(!plonkList.length){ | |
let form = document.getElementById("plonkForm"); | |
form.remove(); | |
form = document.createElement("form"); | |
form.setAttribute("id","plonkForm"); | |
buildForm(form,user); | |
document.getElementsByClassName("overlaywin").item(0).appendChild(form); | |
} | |
GM_setValue("plonkList",plonkList.join(",")); | |
} | |
function addToPlonkList(plonkList,user){ | |
let bg = document.createElement("div"); | |
bg.className = "overlaybg"; | |
document.body.appendChild(bg); | |
let win = document.createElement("div"); | |
win.className = "overlaywin"; | |
let form = document.createElement("form"); | |
form.setAttribute("id","plonkForm"); | |
buildForm(form,user); | |
win.appendChild(form); | |
document.body.appendChild(win); | |
return false; | |
} | |
let articles = document.getElementsByTagName('article'); | |
for (let i = 0; i < articles.length; i++) { | |
let userLink = articles[i].getElementsByClassName('meta').item(0).getElementsByTagName('a').item(0); | |
let span = document.createElement("span"); | |
let sup = document.createElement("sup"); | |
let a = document.createElement("a"); | |
let txt = document.createTextNode(" Plonk"); | |
a.appendChild(txt); | |
a.addEventListener("click",function(){addToPlonkList(plonkList,userLink.textContent);},true); | |
a.href = "#"; | |
sup.appendChild(a); | |
span.appendChild(sup); | |
userLink.parentNode.insertBefore(span,userLink.nextSibling); | |
if(plonkClosedAccounts){ | |
GM_xmlhttpRequest({ | |
method: "GET", | |
url: userLink.href, | |
context: articles[i], | |
onload: function(response) { | |
if(response.responseText.match(/Compte fermé/)){ | |
response.context.style.display = "none"; | |
let userLink =response.context.getElementsByClassName('meta').item(0).getElementsByTagName('a').item(0); | |
let listModified = false; | |
let index = plonkList.indexOf(userLink.textContent); | |
if(index !== -1){ | |
plonkList.splice(index,1); | |
listModified = true; | |
} | |
let re = new RegExp("https://linuxfr.org/users/(.*)$"); | |
let matches = userLink.href.match(re); | |
if(matches !== null){ | |
index = plonkList.indexOf(matches[1]); | |
if(index !== -1){ | |
plonkList.splice(index,1); | |
listModified = true; | |
} | |
} | |
if(listModified){ | |
GM_setValue("plonkList",plonkList.join(",")); | |
} | |
} | |
} | |
}); | |
} | |
for(let j = 0; j < plonkList.length; j++){ | |
if (userLink.textContent === plonkList[j] || userLink.href === "https://linuxfr.org/users/"+plonkList[j]){ | |
articles[i].style.display = "none"; | |
break; | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment