// ==UserScript==
// @name           UserScripts pre-fill original URL when updating script
// @namespace      http://henrik.nyh.se
// @description    When updating your UserScripts scripts, pre-fill the input box with the original source location.
// @include        http://www.userscripts.org/scripts/show/*
// ==/UserScript==

// Original script locations:
// * http://henrik.nyh.se/greasemonkey/
// * http://userscripts.org/scripts/show/1822
//


// match condition by LouCypher@UserScripts.org

/*	TO DO:
	Either
	- Make "Update this script" submit the form
	or
	- Add a "(From here)" link next to "Update this script", to do this (DONE!)
*/

var myScript = document.getElementById('manage');
if(!myScript) return;

var loc = document.getElementById('script_location');

var as = document.getElementsByTagName("a");

for (var i = 0; i < as.length; i++)
	if ((as[i].innerHTML == 'Original Source Location') &&
     (!as[i].href.match(/\/scripts\/show\/\d+/))) {
    loc.value = as[i].href;

    var submitter = document.createElement('a');
    submitter.href = as[i].href;
    submitter.title = 'Update this script from Original Source Location';
    submitter.setAttribute('onclick', 'return false;');
    submitter.innerHTML = 'from original location<br>';
    submitter.addEventListener('click', function(event) {
      document.getElementById('updatescript')
        .getElementsByTagName('form')[0].submit();
    }, false);

    myScript.insertBefore(submitter, myScript.getElementsByTagName('a')[0]
      .nextSibling.nextSibling);
  }

