feat: add hnpw-extractor

This commit is contained in:
Glenn Y. Rolland 2023-12-11 23:45:29 +01:00
parent e7cc3c947f
commit b5052ea0ef

View file

@ -0,0 +1,77 @@
// ==UserScript==
// @name GX.HNPW.Extractor
// @match https://vosocc.unocha.org/Report.aspx?*
// @version 1.0
// ==/UserScript==
// ex: https://vosocc.unocha.org/Report.aspx?page=o0t9pExuBwMwml9Wkc49cgxxxequalxxxequal
(() => {
const CONSOLE_PREFIX = "GX.HNPW.Extractor: ";
const ID_PREFIX = "gxHnpwExtractor-";
//
let bodyElem = document.querySelector("body");
function download(text){
let link = document.createElement("a");
let file = new Blob([text], { type: 'text/plain' });
link.href = URL.createObjectURL(file);
link.download = "hnpw-titles.txt";
link.click();
URL.revokeObjectURL(link.href);
}
function runExtract(){
console.log(CONSOLE_PREFIX + "Begin extract()");
let tableElem = document.querySelector("#divPublishedSessions");
let trElemArray = tableElem.querySelectorAll("tr");
let result = "";
for (let trElem of trElemArray) {
console.log(trElem);
let tdElemArray = trElem.querySelectorAll("td");
if (!tdElemArray) { continue; }
if (tdElemArray.length < 4) { continue; }
result += "* " + tdElemArray[1].textContent + "\n";
}
download(result);
console.log(result);
console.log(CONSOLE_PREFIX + "End extract()");
}
function setupTools() {
let padElem = document.createElement('div');
padElem.id = ID_PREFIX + "pad";
padElem.style.position = 'fixed';
padElem.style.zIndex = "9999";
padElem.style.top = '0';
padElem.style.right = '0';
padElem.style.width = '100px';
padElem.style.height = '50px';
padElem.style.border = '1px solid black';
padElem.style.backgroundColor = 'rgba(0,0,0,0.5)';
padElem.style.display = "flex";
padElem.style.justifyContent = "center";
padElem.style.alignItems = "center";
let buttonElem = document.createElement("input");
buttonElem.value = "Extract";
buttonElem.type = "button";
buttonElem.addEventListener("click", runExtract)
// Insertion des éléments dans le body
padElem.appendChild(buttonElem);
bodyElem.appendChild(padElem);
}
function initialize() {
console.log(CONSOLE_PREFIX + "Begin setup()");
let padElem = document.querySelector('#' + ID_PREFIX + "pad");
if (!padElem) {
setupTools();
}
console.log(CONSOLE_PREFIX + "End setup()");
}
initialize();
})();