Skip to content

Instantly share code, notes, and snippets.

@psenough
Last active May 18, 2024 16:20
Show Gist options
  • Save psenough/6a9e2930806dffd29f12f25724a9a16e to your computer and use it in GitHub Desktop.
Save psenough/6a9e2930806dffd29f12f25724a9a16e to your computer and use it in GitHub Desktop.
csdb screenshots
// ==UserScript==
// @name CSDB Screenshots
// @namespace http://tampermonkey.net/
// @version 0.4
// @description try to take over the world!
// @author You
// @match https://csdb.dk/event/*
// @match https://csdb.dk/group/*
// @match https://demozoo.org/productions/new/?*
// @icon https://www.google.com/s2/favicons?sz=64&domain=csdb.dk
// @grant GM_xmlhttpRequest
// ==/UserScript==
function getNextSiblings(elem, filter) {
var sibs = [];
while (elem = elem.nextSibling) {
if (elem.nodeType === 3) continue; // text node
if (!filter || filter(elem)) sibs.push(elem);
}
return sibs;
}
function exampleFilter(elem) {
switch (elem.nodeName.toUpperCase()) {
case 'A':
return true;
default:
return false;
}
}
(function() {
'use strict';
if (window.location.hostname === "demozoo.org") {
// fill out new prod info on demozoo based on csdb page
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
var csdb_id = urlParams.get('csdbid');
if (csdb_id) {
var csdb_url = "https://csdb.dk/release/?id="+csdb_id;
try {
GM.xmlHttpRequest({
method: "GET",
url: csdb_url,
onload: function(response) {
var html_dump = response.responseText;
var doc = document.implementation.createHTMLDocument();
doc.open();
doc.writeln(html_dump);
doc.close();
// get name
var thenamefromcsdb = doc.querySelectorAll("font")[1].innerText;
var title = document.getElementById("id_title");
title.value = thenamefromcsdb;
// get groups
var theb0 = doc.evaluate("//b[text()='Released by :']", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (theb0) {
var gps = getNextSiblings(theb0.nextElementSibling, null);
var names = '';
//console.log(gps);
for (i=0;i<gps.length;i++) {
if (gps[i].nodeName.toUpperCase() == 'BR') break;
if (gps[i].nodeName.toUpperCase() == 'A') {
var thegroupnamefromcsdb = gps[i].innerText;
if (names != '') names += ' + ';
names += thegroupnamefromcsdb;
}
}
var groupname = document.getElementById("id_byline_search");
groupname.value = names;
}
// get date
var theb = doc.evaluate("//b[text()='Release Date :']", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
//var theb = doc.querySelectorAll("b")[1];
if (theb) {
var thedatefromcsdb = theb.nextElementSibling.nextElementSibling.innerText;
var date = document.getElementById("id_release_date");
date.value = thedatefromcsdb;
}
// get prod type
var asm0 = document.getElementById("asmSelect0").getElementsByTagName('*');
//var theb2 = doc.querySelectorAll("b")[2];
var theb2 = doc.evaluate("//b[text()='Type :']", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (theb2) {
var thetypecsdb = theb2.nextElementSibling.nextElementSibling.innerText;
//console.log(thetypecsdb);
for (var j=0; j<asm0.length; j++) {
if (
((asm0[j].innerHTML === " Demo") && (thetypecsdb === "C64 One-File Demo")) ||
((asm0[j].innerHTML === " Demo") && (thetypecsdb === "C64 Demo")) ||
((asm0[j].innerHTML === "→ Cracktro") && (thetypecsdb === "C64 Crack Intro")) ||
((asm0[j].innerHTML === "→ Cracktro") && (thetypecsdb === "C64 Crack")) ||
((asm0[j].innerHTML === " Tool") && (thetypecsdb === "C64 Tool")) ||
((asm0[j].innerHTML === " Slideshow") && (thetypecsdb === "C64 Graphics Collection")) ||
((asm0[j].innerHTML === " Musicdisk") && (thetypecsdb === "C64 Music Collection"))
)
{
asm0[j].selected="selected";
var event = new Event('change');
document.getElementById("asmSelect0").dispatchEvent(event);
}
}
}
// set c64 platform
var asm1 = document.getElementById("asmSelect1").getElementsByTagName('*');
for (j=0; j<asm1.length; j++) {
if (asm1[j].innerHTML == "Commodore 64") {
asm1[j].selected="selected";
var eventp = new Event('change');
document.getElementById("asmSelect1").dispatchEvent(eventp);
}
}
// direct download link
var matchingElement = doc.evaluate("//b[text()='Download :']", doc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (matchingElement) {
var dlfromcsdb = matchingElement.nextElementSibling.nextElementSibling.children[0].children[0].children[0].children[0].innerText;
var url = document.getElementById("id_links-0-url");
url.value = dlfromcsdb;
}
// csdb page link
var addUrlBtn = document.getElementsByClassName("add_button");
var clk = new Event('click');
addUrlBtn[0].dispatchEvent(clk);
var url1 = document.getElementById("id_links-1-url");
url1.value = csdb_url;
}
});
}
catch (err) {
console.log(err);
}
}
} else {
// csdb extensions
var bg_color = "#EAEAEA";
var border_color = "#D5D5D5";
var font_color = "#000000";
var font_face = "tahoma";
var font_size = "11px";
var mode = 0;
addEventListener('keydown', (event) => {
if (event.shiftKey) {
mode += 1;
if (mode > 4) mode = 0;
console.log('changed to mode ' + mode);
//locate(event);
}
});
function locate(event) {
var posx, posy;
var d = find_div();
if (d) {
switch (mode) {
case 0:
default:
posx = event.clientX + window.pageXOffset;
posy = event.clientY + window.pageYOffset;
d.style.top = (posy - 223) + "px";
d.style.left = (posx + 15) + "px";
break;
case 1:
d.style.top = window.pageYOffset + 10 + "px";
d.style.right = window.pageXOffset + 10 + "px";
break;
case 2:
d.style.top = window.pageYOffset + 10 + "px";
d.style.left = window.pageXOffset + 10 + "px";
break;
case 3:
d.style.bottom = - window.pageYOffset - window.innerHeight + document.body.scrollHeight + 10 + "px";
d.style.right = window.pageXOffset + 10 + "px";
break;
case 4:
d.style.bottom = - window.pageYOffset - window.innerHeight + document.body.scrollHeight + 10 + "px";
d.style.left = window.pageXOffset + 10 + "px";
break;
}
}
}
function find_div() {
return document.getElementById("link_tt");
}
function setImageSource(tt_image, demo_num, ext) {
var scrnum = demo_num;
var scrpath = scrnum.slice(0, -3);
tt_image.src = "https://csdb.dk/gfx/releases/"+scrpath+"000/"+scrnum+"."+ext;
}
function addImage(tt_image, tt_div) {
tt_div.innerHTML = "";
tt_div.appendChild(tt_image);
}
function create_div(event,elem) {
var tt_div = document.createElement("div");
tt_div.setAttribute("id", "link_tt");
tt_div.setAttribute("style", "background:" + bg_color + ";border:1px solid " + border_color + ";padding:2px;color:" + font_color + ";font-family:" + font_face + ";font-size:" + font_size + ";position:absolute;z-index:1000;");
var demo_num = elem.href.replace(/.*?id=([0-9]*).*/,'$1');
var tt_image = new Image();
setImageSource(tt_image, demo_num, "png");
tryOtherExtensionsIfNeeded(tt_div, tt_image, demo_num, ["gif","jpg"]);
addImage(tt_image, tt_div);
tt_div.style.display = 'none';
document.body.appendChild(tt_div);
locate(event);
}
function tryOtherExtensionsIfNeeded(tt_div, tt_image, demo_num, imageTypes) {
var fired = false;
tt_image.addEventListener("error",function(){
if (fired) {
return;
}
fired = true;
if (imageTypes.length == 0) {
GM_log("We have run out of image types to try!");
} else {
var ext = imageTypes.pop();
// It seems just updating the src was enough to unregister my error event listener (Chrome 32), so now I am going to create and replace the whole image element.
tt_image = new Image();
setImageSource(tt_image, demo_num, ext);
addImage(tt_image, tt_div);
tryOtherExtensionsIfNeeded(tt_div, tt_image, demo_num, imageTypes.slice(0));
}
},false);
tt_image.addEventListener("load",function(){
tt_div.style.display = '';
},false);
}
function kill_window() {
var div = find_div();
if (div) {
div.parentNode.removeChild(div);
}
}
var timer = null;
function resetTimeout(fn) {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (fn) {
timer = setTimeout(fn,300);
}
}
function create_event(elem) {
elem.addEventListener("mouseenter", function(event) { resetTimeout(function(){ create_div(event,elem); }); }, false);
elem.addEventListener("mouseleave", function() { resetTimeout(null); kill_window(); }, false);
elem.addEventListener("mousemove", function(event) { locate(event); }, true);
var demo_num = elem.href.replace(/.*?id=([0-9]*).*/,'$1');
var tt_div = document.createElement("span");
elem.parentElement.appendChild(tt_div);
tt_div.innerHTML = "&nbsp;<a href=\"https://demozoo.org/productions/new/?csdbid="+demo_num+"\" target=\"_blank\" style=\"text-decoration: none;\">📩</a>";
}
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (
links[i].href.indexOf("/release/?id=")>=0
) {
create_event(links[i]);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment