Skip to content

Instantly share code, notes, and snippets.

@frenchie4111
Last active March 23, 2018 08:59
Show Gist options
  • Save frenchie4111/77329500eba5bc2f1534feb48b7c230d to your computer and use it in GitHub Desktop.
Save frenchie4111/77329500eba5bc2f1534feb48b7c230d to your computer and use it in GitHub Desktop.
Adds a room bookmark panel to screeps sidebar
// ==UserScript==
// @name Screeps Room Bookmarks
// @namespace https://screeps.com/
// @version 1.0.0
// @author Mike Lyons, frenchie4111
// @match https://screeps.com/a/*
// @run-at document-idle
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
/* global angular */
const LIST_VERSION = 1;
const LIST_KEY = 'list';
const LIST_VERSION_KEY = 'list_version';
const ADD_ACTION = 'add';
function getList() {
let list_string = GM_getValue( LIST_KEY );
if( !list_string ) return [];
return JSON.parse( list_string );
}
function setList( new_list ) {
GM_setValue( LIST_KEY, JSON.stringify( new_list ) );
}
function appendList( new_item ) {
let list = getList();
console.log( 'list', list );
list.push( new_item );
setList( list );
return list;
}
function initList() {
let version = GM_getValue( LIST_VERSION_KEY );
if( version !== LIST_VERSION ) {
console.log( 'Version changed, resetting list' )
setList( [] );
}
GM_setValue( LIST_VERSION_KEY, LIST_VERSION );
}
function addButton( text, action ){
let gameEl = angular.element( $( 'section.game' ) );
let $rootScope = gameEl.injector().get( '$rootScope' );
if( !$rootScope.__buttons )
{$rootScope.__buttons = [];}
if( !$rootScope.__buttons.find( b=>b.text == text ) ) {
$rootScope.__buttons.push( { text, action } );
console.log( 'Add Button', text, action );
}
}
/**
* initUI creates a new menu item.
* Many thanks to ags131 for providing this
*/
function initUI(){
console.log( 'initUI' );
let asidecontent = $( '.aside-content' );
let gameEl = angular.element( $( 'section.game' ) );
let $rootScope = gameEl.injector().get( '$rootScope' );
let $compile = gameEl.injector().get( '$compile' );
$rootScope.handleButton = function( button ){
if( button.action === ADD_ACTION ) {
console.log( 'Adding', window.location.href );
console.log( appendList( window.location.href ) );
} else {
window.location = button.action;
}
};
let elem = $( '<app:aside-block class="player-custom-ui" heading="Room Bookmarks" visibility-model="customuivisible"><md-button class="md-primary md-hue-1 allwidth" ng-repeat="button in $root.__buttons" ng-click="$root.handleButton(button)">{{button.text}}</md-button></app:aside-block>' );
$compile( elem )( $rootScope );
elem.appendTo( asidecontent );
}
function init(){
if( $( '.player-custom-ui' ).length ) return;
initList();
initUI();
addButton( 'Add Room', ADD_ACTION );
getList()
.forEach( function( link ) {
let link_split = link.split( '/' );
let room_name = link_split.slice( -2 ).join( ' ' );
addButton( room_name, link );
} );
}
$( function(){
setInterval( init,1000 );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment