QT Framework for Grepolis

A script framework for Grepolis

As of 2014-07-28. See the latest version.

  1. // ==UserScript==
  2. // @name QT Framework for Grepolis
  3. // @namespace Quack
  4. // @description A script framework for Grepolis
  5. // @include http://*.grepolis.*/game*
  6. // @icon http://s1.directupload.net/images/140711/eshmcqzu.png
  7. // @version 1.01.00
  8. // @grant GM_listValues
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_deleteValue
  12. // @grant GM_info
  13. // @grant GM_xmlhttpRequest
  14. // ==/UserScript==
  15.  
  16. /************************************************************************
  17. * Main Script
  18. ***********************************************************************/
  19. function main_script (DATA){
  20. /************************************************************************
  21. * Global variables
  22. ***********************************************************************/
  23. var QT = {};
  24. var wID = Game.world_id;
  25. var mID = Game.market_id;
  26. var aID = Game.alliance_id;
  27. var sID = Game.player_id;
  28. var pName = Game.player_name;
  29. /************************************************************************
  30. * Languages
  31. ***********************************************************************/
  32. QT.Lang = {
  33. get : function (a, b) {
  34. if (QT.Lang[mID] != undefined && QT.Lang[mID][a] != undefined && QT.Lang[mID][a][b] != undefined) {
  35. return QT.Lang[mID][a][b]
  36. } else {
  37. return QT.Lang.en[a][b]
  38. }
  39. },
  40. de : {
  41. test : {
  42. teststring : 'Sprache wurde erkannt'
  43. }
  44. },
  45. en : {
  46. test : {
  47. teststring : 'Language detected'
  48. }
  49. }
  50. };
  51. /************************************************************************
  52. * Images
  53. ***********************************************************************/
  54. QT.Images = {};
  55. /************************************************************************
  56. * Links
  57. ***********************************************************************/
  58. QT.Links = {};
  59. /************************************************************************
  60. * Settings
  61. ***********************************************************************/
  62. QT.Settings = {
  63. values : {
  64. "messageOpenAlert" : true,
  65. "startFunction" : true,
  66. },
  67. save : function (name, value) {
  68. QT_saveValue(name, value);
  69. },
  70. delete : function (name) {
  71. QT_deleteValue(name);
  72. },
  73. setValues : function () {
  74. for(var opt in DATA){
  75. QT.Settings.values[opt] = DATA[opt];
  76. }
  77. }
  78. };
  79. /************************************************************************
  80. * Ajax Call functions
  81. ***********************************************************************/
  82. QT.CallAjaxFunction = {
  83. message : {
  84. default : function (event, xhr, settings) {
  85. if (QT.Settings.values.messageOpenAlert)
  86. QT.Functions.messageOpenAlert();
  87. }
  88. }
  89. };
  90. /************************************************************************
  91. * Functions
  92. ***********************************************************************/
  93. QT.Functions = {
  94. messageOpenAlert : function () {
  95. alert("Die Nachrichten wurden geöffnet");
  96. },
  97. testButtons : function () {
  98. $('#ui_box').append('<div id="qt_buttons" style="position: relative;top: 54px;z-index: 100"><button id="qt_save">Save</button><button id="qt_delete">Delete</button></div>');
  99. $("#qt_save").click(function () {
  100. QT.Settings.save("messageOpenAlert", false);
  101. });
  102. $("#qt_delete").click(function () {
  103. QT.Settings.delete("messageOpenAlert");
  104. });
  105. }
  106. };
  107. /************************************************************************
  108. * Observer
  109. ***********************************************************************/
  110. $.Observer(GameEvents.game.load).subscribe('DIO_START', function(e,data){
  111. QT.Settings.setValues();
  112. QT.Functions.testButtons();
  113. $(document).ajaxComplete(function (event, xhr, settings) {
  114. var a = settings.url.split("?");
  115. var b = a[0].substr(6);
  116. var c = a[1].split("&")[1].substr(7);
  117. if (b in QT.CallAjaxFunction && c in QT.CallAjaxFunction[b]) {
  118. QT.CallAjaxFunction[b][c](event, xhr, settings);
  119. }
  120. });
  121. });
  122. }
  123.  
  124.  
  125. /************************************************************************
  126. * Startup Method
  127. ***********************************************************************/
  128. var DATA = {
  129. script_version : GM_info.script.version
  130. };
  131.  
  132. var keys = GM_listValues();
  133. for (var i = 0, key = null; key = keys[i]; i++) {
  134. DATA[key] = GM_getValue(key);
  135. }
  136.  
  137. unsafeWindow.QT_saveValue = function (name, val){
  138. setTimeout(function(){
  139. GM_setValue(name, val);
  140. window.location.reload();
  141. }, 0);
  142. };
  143. unsafeWindow.QT_deleteValue = function (name){
  144. setTimeout(function(){
  145. GM_deleteValue(name);
  146. window.location.reload();
  147. },0, name);
  148. };
  149.  
  150. if(typeof exportFunction == 'function'){
  151. exportFunction(unsafeWindow.QT_saveValue, unsafeWindow, {defineAs: "QT_saveValue"});
  152. exportFunction(unsafeWindow.QT_deleteValue, unsafeWindow, {defineAs: "QT_deleteValue"});
  153. }
  154.  
  155. function appendScript () {
  156. if (unsafeWindow.Game) {
  157. var QT_script = document.createElement('script');
  158. QT_script.type ='text/javascript';
  159. QT_script.textContent = main_script.toString() + "\n main_script("+ JSON.stringify(DATA) +");";
  160. document.body.appendChild(QT_script);
  161. } else {
  162. setTimeout(function(){
  163. appendScript();
  164. }, 100);
  165. }
  166. }
  167.  
  168. appendScript();