Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2016 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/adc571df2986ec316b50abfcf1986726 to your computer and use it in GitHub Desktop.
Save anonymous/adc571df2986ec316b50abfcf1986726 to your computer and use it in GitHub Desktop.
Plonking userscript for Linuxfr.org
// ==UserScript==
// @name DLFPlonk
// @namespace DLFPlonk
// @include https://linuxfr.org/*
// @version 2
// @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=[];
}
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:200px}");
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 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);
GM_xmlhttpRequest({
method: "GET",
url: userLink.href,
context: articles[i],
onload: function(response) {
if(response.responseText.match(/Compte fermé/)){
response.context.style.display="none";
}
}
});
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