[TS] USO-Updater

An advanced USO Script Updater

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/3636/11010/%5BTS%5D%20USO-Updater.js

  1. // ==UserScript==
  2. // @name [TS] USO-Updater
  3. // @namespace TimidScript
  4. // @description An advanced USO Script Updater
  5. // @usage Give credit to original author and link
  6. // @exclude *
  7. // @version 1.0.11 (8080)
  8. // ==/UserScript==
  9.  
  10.  
  11. /* Information
  12. ********************************************************************************************
  13. Copyright © TimidScript
  14. TimidScript's Homepage: http://userscripts.org:8080/users/100610
  15. Script's Homepage: http://userscripts.org:8080/scripts/show/159301
  16.  
  17. ----------------------------------------------
  18. Description
  19. ----------------------------------------------
  20. USO-Updater is an advance script updater that provides more control than the one provided by
  21. GreaseMonkey.
  22.  
  23. Press "Ctrl+Alt+U" to bring up Update Menu
  24. For information on how to use this script please visit:
  25. http://userscripts.org:8080/scripts/show/159301
  26. Though no longer anything like the original, this script was based on "AEG AutoUpdater"
  27. and released under original license: http://creativecommons.org/licenses/by-nc-sa/3.0/
  28. Original Script: http://userscripts.org:8080/scripts/show/75442/
  29.  
  30. ----------------------------------------------
  31. Version History
  32. ----------------------------------------------
  33. 1.0.11
  34. - Removed duplicated notice to service provider
  35.  
  36. 1.0.10
  37. - Show your appreciation message added
  38. - USO-Updater Link Added
  39. - @exclude * instead of @include
  40.  
  41. 1.0.9
  42. - Bug Fix: Asynchronous, delayed checking to fix issues with "@run-at document-start", which
  43. fails due to waiting for document to load.
  44.  
  45. 1.0.8
  46. - Counter for iForm resize added.
  47. - Added Comments
  48.  
  49. 1.0.7
  50. - Syntax Fix
  51. - To ease the load on userscript.org the default check interval was increased from 2 days to 5
  52. - Increase delay before resize to 1000ms
  53.  
  54. 1.0.6
  55. - Bug Fix: Now checks if document is loaded first. Issue caused by "document-start" metatag.
  56.  
  57. 1.0.5
  58. - Bug Fix: Did not set the infoBox text innerHTML
  59.  
  60. 1.0.4
  61. - Background colour for the iframe's document body is set to white.
  62. - iframe size is set after a timeout delay.
  63. - Added a header and changed the colour of the USO-Updater menu
  64.  
  65. 1.0.3
  66. - Changed the GM saved values to something more unique.
  67.  
  68. 1.0.2
  69. - @updateinfo has become versioninfo
  70. - Displays if versions are the same in update window
  71.  
  72. 1.0.1
  73. - Initial Release
  74. *****************************************************************************************************/
  75.  
  76. var Counter = 0;
  77.  
  78. var USOUpdater =
  79. {
  80. currentMetaData: null,
  81. newMetaData: null,
  82. checkInterval: 5,
  83. updateWindow: 0,
  84. ID: null,
  85.  
  86. //Checks if online USOVersion number is same
  87. check: function (force) {
  88. if (this.checkUpdateNeeded() || force) {
  89. console.info("Checking for update for: " + this.currentMetaData["name"]);
  90. GM_xmlhttpRequest({
  91. method: "GET",
  92. url: "http://userscripts.org:8080/scripts/source/" + this.currentMetaData["uso:script"] + ".meta.js",
  93. //url: "http://blue/uso/script/meta" + this.currentMetaData["uso:script"] + ".meta.js",
  94. onload: function (response) {
  95. USOUpdater.newMetaData = USOUpdater.parseMeta(response.responseText);
  96. var newVersion = USOUpdater.newMetaData['uso:version'];
  97. if (force || (newVersion !== undefined &&
  98. USOUpdater.currentMetaData !== undefined &&
  99. USOUpdater.currentMetaData['uso:version'] !== undefined &&
  100. Number(newVersion) > Number(USOUpdater.currentMetaData['uso:version']))) {
  101. USOUpdater.showUpdateWindow(USOUpdater.newMetaData);
  102. }
  103. }
  104. });
  105. }
  106. },
  107.  
  108. //Checks if online check is needed by checking last time online check was made
  109. checkUpdateNeeded: function () {
  110. this.checkInterval = GM_getValue("USO-Updater: CheckInterval", this.checkInterval);
  111. if (this.checkInterval == 0) return;
  112.  
  113. var now = Math.round(new Date().getTime() / 1000); // was milliseconds
  114. var lastCheck = GM_getValue('USO-Updater: LastCheck', 0);
  115. GM_setValue('USO-Updater: LastCheck', now); // update
  116. //console.log(now > lastCheck + this.checkInterval * 86400);
  117. return now > lastCheck + this.checkInterval * 86400;
  118. },
  119.  
  120. //Parse through the script metadata
  121. parseMeta: function (raw_metadata) {
  122. var lines = raw_metadata.split('\n');
  123. var metadata = {};
  124. var that = this;
  125. for (var i = 0; i < lines.length; i++) {
  126. lines[i].replace(/\s*\/\/\s*@([^ ]+)\s+(.+)/, function (all, key, value) {
  127. key = key.toLowerCase();
  128. metadata[key] = value;
  129. switch (key) {
  130. case "interval":
  131. if (!isNaN(value) && value > 0 && value < 8) value = GM_setValue("USO-Updater: CheckInterval", GM_getValue("USO-Updater: CheckInterval", value));
  132. break;
  133. }
  134. });
  135. }
  136. return metadata;
  137. },
  138.  
  139. getCurrentMeta: function () {
  140. this.currentMetaData = this.parseMeta(GM_getResourceText('meta'));
  141. this.ID = USOUpdater.currentMetaData["uso:script"] + new Date().getTime();
  142. //console.log(USOUpdater.ID);
  143. },
  144.  
  145. //Shows update window
  146. showUpdateWindow: function () {
  147. if (document.getElementById("updateWindow")) return;
  148. if (!USOUpdater.newMetaData) {
  149. USOUpdater.check(true);
  150. return;
  151. }
  152.  
  153. var iframe = document.createElement("iframe");
  154. iframe.id = "updateWindow";
  155. iframe.setAttribute("style", "position:fixed; right: 15px; bottom: 15px; text-align:center; z-index: 9999;");
  156. iframe.onload = function () {
  157. iDoc = iframe.contentDocument || iframe.contentWindow.document;
  158.  
  159. iDoc.body.innerHTML = '<div style="width: 500px; background-color: #ECF6D9; border: 5px ridge; padding: 5px 5px 0 5px;"><div style="text-align: center; border: 1px ridge #808080; padding: 5px;"><span id="cVersion" style="font-weight: bold;">USO-Updater</span><span style="font-size: small;">(1.0.1)</span><a target="_blank"><img alt="home" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABW0lEQVR42mNkwAECAoNZGBkYVgKZCv8ZGFw2rF/7Hps6RmyCvn7+TIwMjIuAstFggf8MJ/4z/HfZvGnjV6IM8Pb2nQakMtGE9wBN8tm6dctPvAZ4eHh1AAXLsRkM9Mo6IBm+Y8f2P1gNcHN1rwBS7Qz4wYL///8n796z6x+KAc5OzkAnM05jIAr8n7h3394CuAEO9g7AwGJcjCtMcBjScODggUZGWxtbb6C+DUARFuI1ww3JZrSxslEEsuSB/nIH0hVE6lzAyMi4EEi/gTvZwsw8AUjNJ9KAxhOnTjagBKKpsQlJBpw+ewbVACMDQxQDGEHhwshwEeJVoBcZ/icgG3DuwnlUAwx09dBdkHjh8qUFUDkHILUf2QCgHKoBulraGAZcvnZ1AVQOwwCgHKoBWuoaGAZcu3ljAVQOwwCgHKoB6iqqIEW7gJgVZsDNO7cXIMnBDPgPlQNFIwMAOj56D6356V8AAAAASUVORK5CYII=" onmouseover="this.style.backgroundColor=\'#FF0\';" onmouseout="this.style.backgroundColor=null;" /></a><div style="text-align: left; font-size: small; color: gray;">Show your appreciation for the script\'s author by writing a <a id="usoSReview">review</a> and becoming a <a id="usoSFan">fan</a>.</div></div><div style="width: auto; background-color: #D5DBCF; border: 1px ridge #808080;"><fieldset style="margin-bottom: 5px;"><legend style="color: #07770D; font-weight: bold;"><span>New Version: </span><span id="nVersion" style="color: #000;">USO-Updater</span><span style="font-size: small;">(1.0.2 BETA)</span></legend><div id="infoBox" style="padding: 5px; background-color: #FFF; border: 1px ridge #808080;">No version information provided</div><div id="warnBeta" style="background-color: #D4C6D7; padding: 0 5px 0 5px; border: 1px ridge #808080; display: none;"><label style="color: #000;">WARNING: New version is a BETA release</label></div><div id="warnAlpha" style="background-color: #D4C6D7; padding: 0 5px 0 5px; border: 1px ridge #808080; display: none;"><label style="color: #F00;">WARNING</label><label style="color: #000;">: New version is an ALPHA release</label></div></fieldset></div><div><form id="USOUpdaterForm"><label>Check for update<select id="intervalLength"><option>never</option><option>everyday</option><option>every 2 days</option><option>every 3 day</option><option>every 4 days</option><option>every 5 day</option><option>every 6 days</option><option>every 7 days</option></select></label><input type="submit" name="update" value="Update" onclick="this.parentNode.name = this.name;" style="float: right" /><input type="submit" name="cancel" value="Cancel" onclick="this.parentNode.name = this.name;" style="float: right" /></form></div></div>'
  160. + '<div style="border: 1px ridge #808080; text-align: center; width:100%; font: bold;">(Press "Ctrl+Alt+U" to access this window)</div>'
  161. + '<div style="text-align: left; font-size:small; color:gray; margin-left: 10px;">Service provided by <a href="http://userscripts.org:8080/scripts/show/159301">USO-Updater script.</a></div>';
  162.  
  163. var el = iDoc.getElementById("cVersion");
  164. el.textContent = USOUpdater.currentMetaData["name"];
  165. el.nextElementSibling.textContent = "(" + USOUpdater.currentMetaData["version"] + ")";
  166.  
  167. el = iDoc.getElementById("nVersion");
  168. el.textContent = USOUpdater.newMetaData["name"];
  169. el.nextElementSibling.textContent = "(" + USOUpdater.newMetaData["version"] + ")";
  170.  
  171. iDoc.getElementById("usoSReview").href = "http://userscripts.org:8080/scripts/reviews/" + USOUpdater.currentMetaData["uso:script"];
  172. iDoc.getElementById("usoSFan").href = "http://userscripts.org:8080/scripts/fans/" + USOUpdater.currentMetaData["uso:script"];
  173.  
  174. try {
  175. if (USOUpdater.currentMetaData['uso:version'] === USOUpdater.newMetaData['uso:version']) el.previousElementSibling.textContent = "Same Version: ";
  176. }
  177. catch (err) { };
  178.  
  179. if (USOUpdater.newMetaData["versioninfo"]) iDoc.getElementById("infoBox").innerHTML = USOUpdater.newMetaData["versioninfo"];
  180.  
  181. el = iDoc.getElementById("intervalLength");
  182. el.selectedIndex = USOUpdater.checkInterval;
  183. el.onchange = function () { USOUpdater.checkInterval = el.selectedIndex; GM_setValue("USO-Updater: CheckInterval", USOUpdater.checkInterval); };
  184.  
  185. try {
  186. iDoc.getElementById("warnBeta").style.display = (USOUpdater.newMetaData["version"].match(/beta/gi)) ? null : "none";
  187. }
  188. catch (err) { };
  189. try {
  190. iDoc.getElementById("warnAlpha").style.display = (USOUpdater.newMetaData["version"].match(/alpha/gi)) ? null : "none";
  191. }
  192. catch (err) { };
  193.  
  194.  
  195. iDoc.body.setAttribute("style", "background-color:white;");
  196. iframe.style.width = (iDoc.body.firstElementChild.offsetWidth + 15) + "px";
  197. iframe.style.height = (iDoc.body.scrollHeight) + "px";
  198. //iframe.style.width = (iDoc.body.scrollWidth) + "px";
  199. //iframe.style.height = (iDoc.body.firstElementChild.offsetHeight + 20) + "px";
  200.  
  201. //Sometimes the resize fails. Small delay before resizing should fix it.
  202.  
  203. var intervalID = setInterval(function (iframe, iDoc) {
  204. iframe.style.width = (iDoc.body.firstElementChild.offsetWidth + 15) + "px";
  205. iframe.style.height = (iDoc.body.scrollHeight) + "px";
  206. Counter++;
  207. if (Counter == 10)
  208. {
  209. clearInterval(intervalID);
  210. }
  211. }, 250, iframe, iDoc);
  212. iDoc.getElementsByTagName("a")[0].href = "http://userscripts.org:8080/scripts/show/" + USOUpdater.currentMetaData["uso:script"];
  213. iDoc.getElementById("USOUpdaterForm").onsubmit = USOUpdater.formsumbit;
  214. }
  215. document.body.appendChild(iframe);
  216. },
  217.  
  218. /*
  219. Meant to show Update History but due USO server overload currently not implemented
  220. addToHistory: function (metaData) {
  221. //http://userscripts.org:8080/scripts/version/35445/566329.user.js
  222. //http://userscripts.org:8080/scripts/version/35445/566329.meta.js
  223. },
  224. */
  225.  
  226. //Opens Update Window
  227. formsumbit: function (e) {
  228. if (e.target.name == "update") {
  229. //console.log("Update window open");
  230. //window.open("http://userscripts.org:8080/scripts/source/" + USOUpdater.currentMetaData["uso:script"] + ".user.js", "_target");
  231. window.open("http://userscripts.org:8080/scripts/source/" + USOUpdater.currentMetaData["uso:script"] + ".user.js", "_self");
  232. }
  233.  
  234. document.body.removeChild(document.getElementById("updateWindow"));
  235. },
  236.  
  237. //Adds menu option to the USO Update Menu that is accessed through Ctrl+Alt+U.
  238. //This only appears if there is more than one active script that utilises the script.
  239. menuItemAdd: function () {
  240. var UpdaterMenu = document.getElementById("USOUpdaterMenu");
  241. if (!UpdaterMenu) {
  242. var UpdaterMenu = document.createElement("div");
  243. UpdaterMenu.id = "USOUpdaterMenu";
  244. UpdaterMenu.setAttribute("style", "z-index: 9999; position: fixed; top: 15px; right: 15px; display: none; background-color: #F00; padding: 10px; border: 5px solid #000;");
  245. var header = document.createElement("div");
  246. header.textContent = "USO-Updater Menu";
  247. header.setAttribute("style", "text-align:center; color:white;font-weight:bold; margin-bottom:5px;");
  248. UpdaterMenu.appendChild(header);
  249.  
  250. var btn = document.createElement("input");
  251. btn.type = "button";
  252.  
  253. btn.value = "Exit";
  254. btn.setAttribute("style", "display: block; width: 100%; margin-top: 10px;");
  255. btn.onclick = function () { UpdaterMenu.style.display = "none"; };
  256. UpdaterMenu.appendChild(btn);
  257. }
  258. var btn = document.createElement("input");
  259. btn.type = "button";
  260. btn.value = USOUpdater.currentMetaData["name"] + " (" + USOUpdater.currentMetaData["version"] + ")";
  261. btn.name = USOUpdater.ID;
  262. btn.setAttribute("style", "display: block; width: 100%;");
  263. btn.onclick = function (e) {
  264. //console.log(USOUpdater.ID, USOUpdater.currentMetaData["name"]);
  265. document.getElementById("USOUpdaterMenu").style.display = "none";
  266. if (USOUpdater.ID == e.target.name)
  267. USOUpdater.showUpdateWindow();
  268. };
  269.  
  270. UpdaterMenu.insertBefore(btn, UpdaterMenu.lastElementChild);
  271. document.body.appendChild(UpdaterMenu);
  272. },
  273.  
  274. //Captures Key presses. (Ctrl+Alt+U)
  275. keydown: function (e) {
  276. var key = e.keyCode;
  277. //console.log(USOUpdater.currentMetaData['uso:version'], USOUpdater.newMetaData['uso:version']);
  278.  
  279. if (e.ctrlKey & e.altKey & e.keyCode == 85) {
  280. e.stopImmediatePropagation(); //No need for this.
  281. var updaterMenu = document.getElementById("USOUpdaterMenu");
  282. if (updaterMenu.getElementsByTagName("input").length > 2) {
  283. updaterMenu.style.display = null;
  284. var updateWindow = document.getElementById("updateWindow")
  285. if (updateWindow) document.body.removeChild(updateWindow);
  286. }
  287. else USOUpdater.showUpdateWindow();
  288. }
  289. }
  290. };
  291.  
  292.  
  293.  
  294. if (window.self === window.top)
  295. {
  296. setTimeout(function ()
  297. {
  298. var interval = setInterval(
  299. function ()
  300. {
  301. if (document.readyState != "loading") //interactive complete
  302. {
  303. clearInterval(interval);
  304. USOUpdater.getCurrentMeta();
  305. USOUpdater.menuItemAdd();
  306. document.onkeydown = USOUpdater.keydown;
  307. USOUpdater.check();
  308. }
  309. }
  310. , 500);
  311. }, 500);
  312. }