Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Save Created Games
- // @namespace ttacreategame
- // @description Save games under Create Game
- // @include http://www.ttadvance.ca/creategame.php
- // @author Triad Freak
- // @version 0.6
- // @grant none
- // ==/UserScript==
- function loadSavedGames(){
- var sg = localStorage.getItem('savedGames');
- if(sg===null) sg = '[]';
- return JSON.parse(sg);
- }
- function saveSavedGames(sg){
- localStorage.setItem('savedGames', JSON.stringify(sg));
- location.reload();
- }
- function addSeparators(n){
- var i, str = '';
- for(i=0; i<n; i++) str += String.fromCharCode(160);
- maintp.appendChild(document.createTextNode(str));
- }
- function addBrs(n){
- for(var i=0; i<n; i++) maintp.appendChild(document.createElement('br'));
- }
- function createForm(action, method){
- var form = document.createElement('form');
- form.setAttribute('action', action);
- form.setAttribute('method', method);
- form.setAttribute('style', 'display: inline-block;');
- return form;
- }
- function createInput(type, name, value){
- var input = document.createElement('input');
- input.setAttribute('type', type);
- input.setAttribute('name', name);
- input.setAttribute('value', value);
- return input;
- }
- function addInputExtra(id, placeholder, input){
- input.setAttribute('id', id);
- input.setAttribute('placeholder', placeholder);
- return input;
- }
- function decorateButton(button){
- button.setAttribute('style', 'border: #990000; border-style: solid; borde' +
- 'r-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; b' +
- 'order-left-width: 1px ; color: #FFFFFF; background-color: #CC0000');
- return button;
- }
- function getMainForm(){
- var i, forms = document.getElementsByTagName('form');
- for(i=0; i<forms.length; i++)
- if(forms[i].getAttribute('action')==='create.php') return forms[i];
- return null;
- }
- function getMainFormO(name){
- var i, f = {}, c = mainf.getElementsByTagName('input');
- f.name = name;
- for(i=0; i<c.length; i++) if(c[i].checked) f[c[i].name] = c[i].value;
- return f;
- }
- function saveRuleSet(id){
- var sg = loadSavedGames();
- if(id==-1) sg.push(getMainFormO(document.getElementById('rs-1').value));
- else sg[id] = getMainFormO(sg[id].name);
- saveSavedGames(sg);
- }
- function addNewRuleSet(){
- var e = addInputExtra('rs-1', 'name', createInput('text', '', ''));
- maintp.appendChild(e);
- addSeparators(1);
- e = decorateButton(createInput('button', '', 'add new'));
- e.addEventListener('click', function(){saveRuleSet(-1)}, true);
- maintp.appendChild(e);
- }
- function clearAllRuleSets(){
- if(!window.confirm('Clear all rule sets?')) return;
- localStorage.setItem('savedGames', '[]');
- location.reload();
- }
- function addClearAllButton(){
- var e = decorateButton(createInput('button', '', 'clear all'));
- e.addEventListener('click', clearAllRuleSets, true);
- maintp.appendChild(e);
- }
- function addRuleSetForm(r){
- var k, f = createForm('create.php', 'post');
- for(k in r) if(k!=='name') f.appendChild(createInput('hidden', k, r[k]));
- f.appendChild(decorateButton(createInput('submit', '', r['name'])));
- maintp.appendChild(f);
- }
- function delRuleSet(id){
- var sg = loadSavedGames();
- sg.splice(id, 1);
- saveSavedGames(sg);
- }
- function addRuleSetDelete(id){
- var e = decorateButton(createInput('button', '', 'delete'));
- e.addEventListener('click', function(){delRuleSet(id);}, true);
- maintp.appendChild(e);
- }
- function addRuleSetAssign(id){
- var e = decorateButton(createInput('button', '', 'assign'));
- e.addEventListener('click', function(){saveRuleSet(id);}, true);
- maintp.appendChild(e);
- }
- function viewRuleSet(id){
- var sg = loadSavedGames();
- if(sg.length<=id) return;
- var i, f = sg[id], c = mainf.getElementsByTagName('input');
- for(i=0; i<c.length; i++){
- if(typeof f[c[i].name] !== "undefined" && f[c[i].name] == c[i].value)
- c[i].checked = true;
- else c[i].checked = false;
- }
- }
- function addRuleSetView(id){
- var e = decorateButton(createInput('button', '', 'view'));
- e.addEventListener('click', function(){viewRuleSet(id);}, true);
- maintp.appendChild(e);
- }
- function changeRuleSetName(id){
- var sg = loadSavedGames();
- if(sg.length<=id) return;
- sg[id].name = document.getElementById('rsn'+id).value;
- saveSavedGames(sg);
- }
- function addRuleSetChangeName(id){
- var e = addInputExtra('rsn'+id, 'new name', createInput('text', '', ''));
- maintp.appendChild(e);
- addSeparators(1);
- e = decorateButton(createInput('button', '', 'change name'));
- e.addEventListener('click', function(){changeRuleSetName(id);}, true);
- maintp.appendChild(e);
- }
- function viewSavedRuleSets(){
- var sg = loadSavedGames();
- for(var i=0; i<sg.length; i++){
- addBrs(1);
- addRuleSetForm(sg[i]);
- addSeparators(2);
- addRuleSetChangeName(i);
- addSeparators(2);
- addRuleSetAssign(i);
- addSeparators(2);
- addRuleSetView(i);
- addSeparators(2);
- addRuleSetDelete(i);
- }
- }
- var mainf = getMainForm();
- var maint = document.getElementById('Main');
- if(mainf!==null && maintp!==null){
- var maintp = maint.parentElement;
- addBrs(1);
- addNewRuleSet();
- addSeparators(2);
- addClearAllButton();
- addBrs(1);
- viewSavedRuleSets();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement