Skip to content

Instantly share code, notes, and snippets.

@Sitethief
Last active June 25, 2022 20:27
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 Sitethief/26c115b4976a2313e265df634d214706 to your computer and use it in GitHub Desktop.
Save Sitethief/26c115b4976a2313e265df634d214706 to your computer and use it in GitHub Desktop.
NSContainerSwitcherQuickMenu - Inserts a quickmenu for opening a page in another container.
// ==UserScript==
// @name NSContainerSwitcherQuickMenu
// @namespace sitethiefs-ns-scripts
// @version 0.1.6
// @description Inserts a quickmenu for opening a page in another container.
// @author Sitethief of Vylixan
// @copyright GPLv3 https://www.gnu.org/licenses/gpl-3.0.html
// @match *www.nationstates.net/*page=deck*card=*
// @require https://craig.global.ssl.fastly.net/js/mousetrap/mousetrap.min.js?a4098
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
let options = {
containerMethod: 'container', // set this to 'container' if you use the 'containerise (container)' type, and to 'nation' if you use 'containerise (nation)' as type.
stripOldContainer: true, // this removes any existing container parts of the current url, leave true if you're unsure
target: '_blank', // _blank for opening a new page with the card, _self for opening the card in the same page
}
// These are the containers used in the quickmenu
// example_container_key_1 should be the name that's used as the container key
// Example Container Name 1 can be literally any text you want
// So you have to list all the containers you want to use in the quickmenu here
let containerList = {
'example_container_key_1': 'Example Container Name 1',
'example_container_key_2': 'Example Container Name 2',
};
function noinput_mousetrap(event) {
if (event.target.classList.contains("mousetrap")) {
event.preventDefault();
event.stopPropagation();
}
}
let cardElement = document.getElementById("deck-single-card");
let location = window.location.href;
let body = document.getElementById("loggedin");
if (body) {
let currentNationKey = body.dataset.nname;
if (cardElement) {
let quickmenuContainer = document.createElement("div");
quickmenuContainer.id = 'quickmenuContainer';
quickmenuContainer.style.margin = '-100px 0 0 100px';
quickmenuContainer.style.fontSize = '1.2em';
quickmenuContainer.style.fontFamily= "'Courier New', monospace";
let quickmenuTitle = document.createElement("h3");
quickmenuTitle.innerHTML = 'Container Quickmenu';
quickmenuContainer.appendChild(quickmenuTitle);
let n = 0;
Object.keys(containerList).forEach(function(containerKey) {
let chr = (n+10).toString(36);
n++;
let containerName = chr + ' - ' + containerList[containerKey];
let quickmenuLink = document.createElement("a");
let newLocation = location;
// strip old container references
if (options.stripOldContainer === true) {
newLocation = newLocation.replace(options.containerMethod + '=' + currentNationKey + '/', '');
}
// Create link
newLocation = newLocation + '/' + options.containerMethod + '=' + containerKey + '/';
// Prevent double //, this is ugly
newLocation = newLocation.replace('//container', '/container');
quickmenuLink.href = newLocation;
quickmenuLink.innerHTML = containerName;
quickmenuLink.style.display = 'block';
quickmenuLink.style.margin = '5px 0 0';
quickmenuLink.target = options.target;
Mousetrap.bind(['shift+' + chr], function(ev) {
noinput_mousetrap(ev);
quickmenuLink.click();
});
quickmenuContainer.appendChild(quickmenuLink);
});
cardElement.appendChild(quickmenuContainer);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment