Skip to content

Instantly share code, notes, and snippets.

@NuarkNoir
Last active April 21, 2019 12:05
Show Gist options
  • Save NuarkNoir/b4a86a8a542aa66bd6beed35d3316a6d to your computer and use it in GitHub Desktop.
Save NuarkNoir/b4a86a8a542aa66bd6beed35d3316a6d to your computer and use it in GitHub Desktop.
I created this script to simplify data-export from NozomiLa
// ==UserScript==
// @name Nozomi Exporter
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Exporting posts from NL
// @author Nuark
// @match https://nozomi.la/
// @match https://nozomi.la/tag/*
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
window.dataholder = {
init: function(max_c) {
this.container = [];
this.max_c = max_c;
},
addData: function(data) {
this.container.push(data);
console.log(`Dispatched item insertion. ${this.container.length} of this.max_c...`);
if (this.container.length >= this.max_c) {
console.log("Work done. Serializing...");
let jtext = JSON.stringify(this.container);
console.log(this.container);
let winop = window.open("about:blank", "_blank");
winop.document.write('<textarea style="width:80vw;height:90vh">' + jtext + "</textarea>");
winop.blur();
window.focus();
}
}
}
window.switch_selection = function (elem) {
let clicked = elem.dataset.checked === "true";
elem.dataset.checked = !clicked;
return elem;
}
window.btn_enabler = function (e) {
e.preventDefault();
let target = e.target;
while (!target.classList.contains("thumbnail-div")) {
target = target.parentNode;
}
window.switch_selection(target);
}
window.export_tags = function(sidebar) {
let out = "";
let tags = sidebar.querySelectorAll("ul > li > a");
tags.forEach(x => {
if (x.classList.toString() !== "tag") out += x.classList.toString() + ":";
out += x.innerText.replace(/ /g, "_") + " ";
});
out = out.trim();
return out;
}
window.zxc = function() {
let targets = document.querySelectorAll("[data-checked=true]");
window.dataholder.init(targets.length);
targets.forEach(dct => {
let href = dct.querySelector("a").href;
fetch(href).then(r => r.text()).then(text => {
console.log("Executing order for " + href);
let holder = document.createElement("div");
holder.innerHTML = text;
[...holder.querySelectorAll("script"), ...holder.querySelectorAll("link")].forEach(scr => scr.remove())
let sidebar = holder.querySelector("div.sidebar");
let tags = window.export_tags(sidebar);
let img_href = holder.querySelector(".post > a").href;
window.dataholder.addData({href: img_href, tags:tags});
window.switch_selection(dct);
});
});
}
let init = function() {
if (document.querySelectorAll(".thumbnail-div").length === 0) {
setTimeout(init, 1000);
return;
}
let button = document.createElement("button");
button.innerText = "EXECUTE ORDER";
button.style = "width: 100%;";
button.onclick = window.zxc;
let sidebar = document.querySelector("div.sidebar");
sidebar.prepend(button);
let s = document.createElement("style");
s.innerText = "[data-checked=true] { outline: greenyellow 3px dashed; }";
document.head.appendChild(s);
document.querySelectorAll(".thumbnail-div").forEach(tdiv => {
tdiv.onclick = window.btn_enabler;
});
}
init();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment