Dragons Hobbies Forum

General Category => General Discussions => Topic started by: Dragon on March 04, 2021, 00:54:34

Title: Greasemonkey for Homeworlds on SuperDuperGames.org
Post by: Dragon on March 04, 2021, 00:54:34
I've been using Greasemonkey (https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/) and Tampermonkey for a few years for little adjustments to things here and there. Generally, I use it for sites that I have no direct control over, such as adding links to commonly used pages in ticketing systems. This time, I've used it to adjust a form on site that I've used often - superdupergames.org (http://superdupergames.org).

Homeworlds is the most commonly played game on that site, but the site is slow, and the navigation is clunky. Even playing Homeworlds on there is a matter of entering text commands into a form, but the biggest problem that I have with it is that the command form is not even visible on the screen at the same time as the map for the game. Tonight, I created a Greasemonkey script to position the command form and stash near starmap. I've also made that form draggable, in case it's covering up something that you want to see.

Additionally, I've added a link to the Telegram Game in the top nav menu, which is a casual drawing game, like playing the Telephone party game, but with pictures. The Telegram Game is accessible through a small link on the My SDG page, but it's not very noticeable there and I haven't seen it on the navigation menu anywhere else... until now. So here it is:

Code: [Select]
// ==UserScript==
// @name           SDG Enhancements
// @description    Enhancements for the SuperDuperGames site - Float Homeworlds form and stash near starmap and make it draggable. Also add Telegram link to main menu.
// @author         nathaniel@dragonshobbies.com
// @version        1.3.1
// @match          http://superdupergames.org/*
// ==/UserScript==

// Add Telegram link to main menu
var myMenuP = document.querySelector('p');
var myNewA = document.createElement('a');
myNewA.setAttribute('href',"http://superdupergames.org/?page=telegram");
myNewA.setAttribute('style',"font-weight:bold;");
myNewA.innerText = 'Telegram Game';
myMenuP.append(' | ');
myMenuP.appendChild(myNewA);

// Float Homeworlds form and stash near starmap
if(window.location.search.indexOf('page=play_homeworlds') !== -1) {
  var myForm = document.querySelector('form');
  var myNewP = document.createElement('p');
  myNewP.innerText = 'Is this in your way? You\'re in luck! This box is draggable!';
  myNewP.setAttribute('style', 'text-align:center; font-style:italic;');
  var myImg = document.querySelector('table table table img');
  myImg.setAttribute('style', "max-width:100%");
  myForm.setAttribute('style',"position:fixed; width:400px; left:10%; background-color: #c0c0c0; border: 3px groove; cursor:grab; padding: 10px;");
  //myNewP.setAttribute('style',"position:absolute; right:0;");
  myForm.appendChild(myNewP);
  myNewP.appendChild(myImg);
}

// Make the box draggable: source: https://www.w3schools.com/howto/howto_js_draggable.asp
dragElement(myForm);

function dragElement(elmnt) {
  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
  if (document.getElementById(elmnt.id + "header")) {
    // if present, the header is where you move the DIV from:
    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
  } else {
    // otherwise, move the DIV from anywhere inside the DIV:
    elmnt.onmousedown = dragMouseDown;
  }

  function dragMouseDown(e) {
    e = e || window.event;
    e.preventDefault();
    // get the mouse cursor position at startup:
    pos3 = e.clientX;
    pos4 = e.clientY;
    document.onmouseup = closeDragElement;
    // call a function whenever the cursor moves:
    document.onmousemove = elementDrag;
    elmnt.style.cursor = 'move'; // You can do this or use a css class to change the cursor
    return true;
  }

  function elementDrag(e) {
    e = e || window.event;
    e.preventDefault();
    // calculate the new cursor position:
    pos1 = pos3 - e.clientX;
    pos2 = pos4 - e.clientY;
    pos3 = e.clientX;
    pos4 = e.clientY;
    // set the element's new position:
    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
  }

  function closeDragElement() {
    // stop moving when mouse button is released:
    document.onmouseup = null;
    document.onmousemove = null;
    elmnt.style.cursor = 'grab'; // You can do this or use a css class to change the cursor
    elmnt.getElementsByTagName('textarea')[0].focus(); // Fix focus after moving
  }
}

I've also uploaded this script to GitHub (https://github.com/dragon76n/greasemonkey/blob/master/sdgEnhancements.js).

Below are a couple screenshots showing how it looks using my Greasemonkey script on that site.
Title: Re: Greasemonkey
Post by: Dragon on March 09, 2021, 20:12:48
I tried this on a Mac using Chrome with Tampermonkey tonight and my version 1 wouldn't match the site in Tampermonkey.

Since this site is always using http protocol right now and appears to always be working without the www, I'm making the following change (to be modified in my original post).
Code: [Select]
// @match    http://*.?superdupergames.org/*

Changed to:

Code: [Select]
// @match    http://superdupergames.org/*

Apparently  the URL match to work in Tampermonkey along with Greasemonkey didn't like the wildcard stuff to match the beginning of the URL. Also, I tried removing the http protocol and Greasemonkey hated that. Fortunately this works for both.
Title: Re: Greasemonkey for Homeworlds on SuperDuperGames.org
Post by: Dragon on April 20, 2021, 21:06:13
For comparison, here are a couple screenshots showing Homeworlds in progress on SDG without my Greasemonkey script active. The command form is so far away from the star map that you have to scroll completely away from one to see the other.
Title: Re: Greasemonkey for Homeworlds on SuperDuperGames.org
Post by: Dragon on July 15, 2021, 23:52:49
Updated to remove unnecessary color changing code and fix cursor changing for mousedown and mouseup.
Title: Re: Greasemonkey for Homeworlds on SuperDuperGames.org
Post by: Dragon on July 21, 2021, 18:48:05
Version 1.3 updates the focus for input when the form is moved or clicked on. This is not always an issue, but if a user click on some other part of the page, the focus changes and then it's a little tricky to get back in to type in the form otherwise.
Title: Re: Greasemonkey for Homeworlds on SuperDuperGames.org
Post by: Dragon on October 31, 2022, 21:52:22
I just updated the userscript above to Version 1.3.1, adding the description.