Publish Trigger

From Chickenfoot Script Repository

Jump to: navigation, search
// ==UserScript==
// @name Publish Trigger
// @when Pages Match
// @description Publishes a Chickenfoot trigger script on the wiki
// @wiki Publish Trigger
// ==/UserScript==

//choose index of trigger to publish
var triggerIndex = 0;

//get trigger to publish
var trigger = Chickenfoot.gTriggerManager.triggers[triggerIndex];
publishTrigger(trigger);

function publishTrigger(/*Trigger*/trigger) {
  //get name and description of trigger to publish
  var name = trigger.name;
  var description = trigger.description;
  var currentScript = Chickenfoot.SimpleIO.read(trigger.path);
  
  //get trigger wiki name out of trigger metadata
  var map = Chickenfoot.extractUserScriptAttributes(currentScript);
  if(map.wiki) { name = map.wiki; }
  else {
    map = {};
    map.wiki = trigger.name;
    var updatedScript = Chickenfoot.updateAttributes(currentScript, map);
    Chickenfoot.SimpleIO.write(trigger.path, updatedScript);
  }

  //-----------

  //go to chickenfoot scripts wiki page if not already there
  var currentPage = document.location;
  var scriptsWiki = 'http://groups.csail.mit.edu/uid/chickenfoot/scripts/index.php/Main_Page';
  if(document.location != scriptsWiki) { go(scriptsWiki); }

  //go to script section and check if this script already exists there
  //just put all scripts uner 'General Scripts' for now
  var scriptSection = 'General Scripts';
  click(scriptSection);
  var existingLink = find(name + ' link');
  var existingMatch = existingLink.hasMatch && (existingLink.text == name);

  //there is already a trigger called this, ask the user if they want to replace it
  var overwrite = false;
  if(existingMatch) {
    overwrite = window.wrappedJSObject.confirm('Are you sure you want to over-write this existing script?');

    //if don't want to replace it, then return without doing anything
    if(!overwrite) { return; }

    //else open edit tab for just this section
    else{ click(find(name + ' link').element.parentNode.previousSibling.previousSibling.childNodes[1]); }    
 }

  //there is not a script already called this
  else {
    //open edit tab of entire script section
    var editLink = find('edit');
    while(editLink.next != Chickenfoot.EMPTY_MATCH) { editLink = editLink.next; }
    click(editLink);
  }

  //if not logged in, follow the re-direct link to the login page, and leave the user there
  var loginLink = find('login link');
  if (loginLink.hasMatch) {
    click('login link');
    return;
  }

  //find editor box
  var textbox = find('first textbox').element.wrappedJSObject;

  //save existing text, so can add to it
  var existingTxt = textbox.textContent;
  if(overwrite) { existingTxt = ''; }

  //get new text and insert into textarea
  var newTxt = '\n=== [[' + name + ']] ===\n' + description;
  if(overwrite) { newTxt = '=== [[' + name + ']] ===\n' + description; }
  var txtToInsert = existingTxt + newTxt;
  textbox.textContent = txtToInsert;

  //save the page and keep location so can go back to this page when done
  click('save the page button');
  var returnLocation = document.location.toString();
  
  //-----------

  //open edit tab for new page and find editor box\
  click('' + name + ' link');
  textbox = find('first textbox').element.wrappedJSObject;
  if(overwrite || !textbox.hasMatch) { 
    click('edit link');
    textbox = find('first textbox').element.wrappedJSObject;
  }

  //replace the existing text with the current script on disk
  textbox.textContent = '<pre>' + currentScript;

  //save the page
  click('save the page button');

  //return to index page of scripts
  go(returnLocation)

}

Personal tools