Greasy Fork镜像 支持简体中文。

PardusMonkey Abstraction Layer

Userscript library for www.pardus.at

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/7341/30850/PardusMonkey%20Abstraction%20Layer.js

  1. // ==UserScript Library==
  2. // @name Pardusmonkey Assistance Library (PAL)
  3. // @description Provides functions to abstract common tasks for Pardus greasemonkey scripts in a cross-browser fashion.
  4. // @version 1
  5. // @author Richard Broker (Beigeman)
  6. // ==/UserScript Library==
  7.  
  8. /*
  9. * Constructor for the PAL Object.
  10. * scriptName is the name of the script you're registering for use with PAL.
  11. * scriptKey is a unique key obtainable from the following URL: http://grandunifyingalliance.com/gm/doc/id_generator.php
  12. */
  13. function PardusMonkey(scriptName, scriptKey)
  14. {
  15. /* **************************
  16. * Enums
  17. * ************************* */
  18. this.e_logLevel =
  19. {
  20. QUIET : 0,
  21. DEBUG : 1,
  22. VERBOSE : 2
  23. };
  24.  
  25. this.e_configAttach =
  26. {
  27. LEFT : 0,
  28. RIGHT : 1
  29. };
  30.  
  31. this.e_storageType =
  32. {
  33. SESSION : 0,
  34. LOCAL : 1
  35. };
  36.  
  37. this.e_toastStyle = // Different toast styles.
  38. {
  39. SUCCESS : "#BADA55",
  40. ERROR : "#D7546B",
  41. NOTIFY : "#E5DE59"
  42. };
  43.  
  44. this.e_pageVar = // Enum to hold common page variables.
  45. {
  46. USER_LOC : "userloc",
  47. NAV_SIZE : "navAreaSize",
  48. TILE_RES : "tileRes",
  49. IMG_DIR : "imgDir",
  50. USER_ID : "userid",
  51. MILLI_TIME : "milliTime"
  52. };
  53.  
  54. this.e_page = // Enum to hold different pardus pages.
  55. {
  56. GAME : "game.php",
  57. NAV : "main.php",
  58. BUILDING : "building.php",
  59. AMBUSH : "ambush.php",
  60. MSGFRAME : "msgframe.php",
  61. OVERVIEW : "overview.php",
  62. ADVANCED_SKILLS : "overview_advanced_skills.php",
  63. CHAT : "chat.php",
  64. PLANET : "planet.php",
  65. STARBASE : "starbase.php",
  66. BLACK_MARKET : "blackmarket.php",
  67. PLANET_TRADE : "planet_trade.php",
  68. BUILDING_TRADE : "building_trade.php",
  69. STARBASE_TRADE : "starbase_trade.php",
  70. SHIP_EQUIPMENT : "ship_equipment.php",
  71. BULLETIN_BOARD : "bulletin_board.php",
  72. SHIP_2_SHIP : "ship2ship_combat.php",
  73. SHIP_2_NPC : "ship2opponent_combat.php",
  74. LOGOUT : "logout.php",
  75. OPTIONS : "/options.php"
  76. // #TODO add more of these
  77. // #TODO ensure functions which might fail when not on specific pages check their location first.
  78. };
  79.  
  80. /* **************************
  81. * Global Variables
  82. * ************************* */
  83. this.g_logLevel = this.e_logLevel.QUIET;
  84. this.g_scriptName = scriptName;
  85. this.g_scriptKey = scriptKey;
  86. this.g_toastDuration_ms = 3000;
  87. this.g_configInputWidth = "90%";
  88.  
  89. /* **************************
  90. * Private Variables
  91. * ************************* */
  92. var priv_document = document;
  93. var priv_uniqid = (this.g_scriptKey + this.g_scriptName).replace(/\W/g, "_");
  94. var priv_toastName = priv_uniqid + "pardusMonkeyToast";
  95. var priv_toastTimeout = null;
  96. var priv_toastMilliTime = 0;
  97. var priv_currentToast = null;
  98. var priv_pageURL = null;
  99. var priv_partialRefresh = null;
  100. var priv_div = null;
  101. var priv_input = null;
  102. var priv_p = null;
  103. var priv_span = null;
  104. var priv_script = null;
  105. var priv_configObject = null;
  106. var priv_configValues = [];
  107. var priv_configCallback = null;
  108. var priv_userlocIsStale = false;
  109. var priv_mutationConfig = { attributes : true }; // Config for the PR mutation observer.
  110.  
  111. /* **************************
  112. * Public Functions
  113. * ************************* */
  114. /*
  115. * Storage Class Functions
  116. */
  117. this.SetValue = function(name, value, storage)
  118. {
  119. if(!name)
  120. {
  121. this.DebugLog("(FATAL:SetValue) name parameter is required.", this.e_logLevel.QUIET);
  122. return;
  123. }
  124.  
  125. name = priv_uniqid + name;
  126.  
  127. if (typeof(Storage) !== "undefined")
  128. {
  129. value = (typeof value)[0] + value;
  130.  
  131. if (storage === this.e_storageType.SESSION)
  132. {
  133. storage = sessionStorage;
  134. }
  135. else
  136. {
  137. storage = localStorage;
  138. }
  139.  
  140. storage.setItem(name, value);
  141. }
  142. else
  143. {
  144. this.DebugLog("(FATAL:SetValue) HTML5 localStorage support required.", this.e_logLevel.QUIET);
  145. }
  146. };
  147.  
  148. this.GetValue = function(name, defaultValue, storage)
  149. {
  150. if(!name)
  151. {
  152. this.DebugLog("(FATAL:GetValue) name parameter is required.", this.e_logLevel.QUIET);
  153. return null;
  154. }
  155.  
  156. name = priv_uniqid + name;
  157.  
  158. if (typeof(Storage) !== "undefined")
  159. {
  160. if (name)
  161. {
  162. if (storage === this.e_storageType.SESSION)
  163. {
  164. storage = sessionStorage;
  165. }
  166. else
  167. {
  168. storage = localStorage;
  169. }
  170.  
  171. var value = storage.getItem(name);
  172.  
  173. if ((!value) && defaultValue) return defaultValue;
  174. if (!value) return null;
  175.  
  176. var type = value[0];
  177. value = value.substring(1);
  178. switch (type) {
  179. case 'b':
  180. return value === 'true';
  181. case 'n':
  182. return Number(value);
  183. default:
  184. return value;
  185. }
  186. }
  187. }
  188. else
  189. {
  190. this.DebugLog("(FATAL:GetValue) HTML5 localStorage support required.", this.e_logLevel.QUIET);
  191. return null;
  192. }
  193. };
  194.  
  195. /*
  196. * Output Class Functions
  197. */
  198. this.DebugLog = function(msg, verbosity)
  199. {
  200. if (!msg)
  201. {
  202. this.console.log("[" + scriptName + "] (FATAL:DebugLog) : msg parameter is required!");
  203. return;
  204. }
  205.  
  206. if (verbosity === null)
  207. {
  208. verbosity = this.e_logLevel.QUIET;
  209. }
  210.  
  211. if(this.g_logLevel < verbosity)
  212. return;
  213.  
  214. var caller = "";
  215.  
  216. try
  217. {
  218. caller = arguments.callee.caller.name;
  219. }
  220. catch (e) { /* Not the end of the world, we just wont use the function name. */ }
  221.  
  222. if (caller !== "")
  223. {
  224. console.log("[" + scriptName + "] " + caller.toString() + ": " + msg);
  225. }
  226. else
  227. {
  228. console.log("[" + scriptName + "] " + msg);
  229. }
  230. };
  231.  
  232. this.Toast = function(msg, toastStyle)
  233. {
  234. var attach = priv_GetToastAttachPoint();
  235. var toastDiv = attach.getElementById(priv_toastName);
  236.  
  237. // In case the msgframe is reloaded while the toast is showing, note the current time.
  238. priv_toastMilliTime = this.GetPageVariable(this.e_pageVar.MILLI_TIME, attach);
  239. priv_currentToast = { text:msg, style:toastStyle };
  240.  
  241. // No toast has been used yet, so we need to create an element to contain the toasts.
  242. if(!toastDiv)
  243. {
  244. priv_InitialiseToasts();
  245. toastDiv = attach.getElementById(priv_toastName);
  246. }
  247.  
  248. if (toastDiv)
  249. {
  250. if (!toastStyle)
  251. {
  252. toastStyle = this.e_toastStyle.SUCCESS;
  253. }
  254.  
  255. if (priv_toastTimeout !== null)
  256. {
  257. clearTimeout(priv_toastTimeout);
  258. }
  259.  
  260. toastDiv.innerHTML = '<b>' + this.g_scriptName + '</b>: ' + msg + '&nbsp;&nbsp;';
  261. toastDiv.style.background = toastStyle;
  262. toastDiv.style.display = "block";
  263. toastDiv.style.opacity = 1;
  264. priv_toastTimeout = setTimeout(priv_CloseToast, this.g_toastDuration_ms);
  265. }
  266. else
  267. {
  268. this.DebugLog("(FATAL:Toast) Unable to locate toast div");
  269. }
  270. };
  271.  
  272. /*
  273. * Utility Class Functions
  274. */
  275. // Derived from Victoria Axworthy's trick for cross browser userloc retrieval.
  276. this.GetPageVariable = function(variableName, doc)
  277. {
  278. var stripQuotes = false;
  279.  
  280. if(!variableName)
  281. {
  282. this.DebugLog("(FATAL:GetPageVariable) variableName parameter is required.", this.e_logLevel.QUIET);
  283. return null;
  284. }
  285.  
  286. if (!doc)
  287. {
  288. doc = priv_document;
  289. }
  290.  
  291. if ((variableName === this.e_pageVar.USER_LOC) && (priv_userlocIsStale === true))
  292. {
  293. /* Update the userloc variable store in the pardus source, so that GetPageVariable() doesn't break. */
  294. ExecuteInPage(priv_UpdateUserLoc.toString() + "\npriv_UpdateUserLoc();");
  295. priv_userlocIsStale = false;
  296. }
  297. else if (variableName === this.e_pageVar.IMG_DIR)
  298. {
  299. stripQuotes = true;
  300. }
  301.  
  302. variableName += " = ";
  303.  
  304. // We avoid using unsafeWindow by reading the actual script text embedded in the Pardus HTML.
  305. var scripts = doc.getElementsByTagName('script');
  306. for(var i = 0; i < scripts.length; i++)
  307. {
  308. if(!scripts[i].src)
  309. {
  310. if(scripts[i].textContent.indexOf(variableName) !== -1)
  311. {
  312. var line = scripts[i].textContent.split(variableName)[1];
  313. var pageVar = line.split(';')[0];
  314.  
  315. if (pageVar)
  316. {
  317. // These will always be string types probably. #TODO #FIXME
  318. var type = (typeof pageVar)[0];
  319. switch (type)
  320. {
  321. case 'boolean':
  322. return pageVar === 'true';
  323. case 'number':
  324. return Number(pageVar);
  325. default:
  326. if (stripQuotes === true)
  327. {
  328. pageVar = pageVar.replace(/\"/g, '');
  329. }
  330. return pageVar;
  331. }
  332. }
  333. }
  334. }
  335. }
  336. return null;
  337. };
  338.  
  339. this.ExecuteInPage = function(sJavascript)
  340. {
  341. var script = priv_CreateScript();
  342. script.textContent = sJavascript;
  343. priv_document.body.appendChild(script);
  344. priv_document.body.removeChild(script);
  345. };
  346.  
  347. this.GetUniverseName = function()
  348. {
  349. return priv_document.location.hostname.substr(0, priv_document.location.hostname.indexOf('.'));
  350. };
  351.  
  352. /* In the event that the cached "document" is out of date due to DOM manipulations in the user script, this call can be used to update the cached copy.
  353. This method should also reset any other values which have been cached, so the next time they are requested they will be regenerated.
  354. */
  355. this.UpdateCache = function()
  356. {
  357. priv_document = document;
  358. priv_pageURL = null;
  359. priv_div = null;
  360. priv_input = null;
  361. priv_p = null;
  362. priv_span = null;
  363. };
  364.  
  365. this.PageIs = function(page)
  366. {
  367. if (priv_pageURL === null)
  368. {
  369. priv_pageURL = priv_document.location.href.substr(priv_document.location.href.lastIndexOf('/'), priv_document.location.href.length);
  370. }
  371.  
  372. if (priv_pageURL.indexOf(page) < 0)
  373. {
  374. return false;
  375. }
  376. else
  377. {
  378. return true;
  379. }
  380. };
  381.  
  382. /*
  383. * Partial Refresh Class Functions
  384. */
  385. this.PREnabled = function()
  386. {
  387. if (priv_partialRefresh === null)
  388. {
  389. if (priv_document.getElementById('nav'))
  390. {
  391. priv_partialRefresh = true;
  392. }
  393. else
  394. {
  395. priv_partialRefresh = false;
  396. }
  397. }
  398.  
  399. return priv_partialRefresh;
  400. };
  401.  
  402. /* We attach to the AP counter because if we attach to something like document.body and watch for events
  403. in the whole subtree, then other scripts changing things will fire off this observer. If both scripts
  404. are using an observer, then they will enter an infinite loop on each other's changes. */
  405. this.AddPRCallback = function(callbackFunction)
  406. {
  407. if (!callbackFunction)
  408. {
  409. this.DebugLog("(FATAL:AddPRCallback) callbackFunction parameter is required.");
  410. return;
  411. }
  412.  
  413. var mutationTarget = priv_document.getElementById('apsleft');
  414.  
  415. if (this.PageIs(this.e_page.NAV) && (mutationTarget))
  416. {
  417. /* Initialise the obeserver for the first click */
  418. PRObserver = new MutationObserver(function(mutations) {
  419.  
  420. /* Prevent function running multiple times per click. */
  421. PRObserver.disconnect();
  422.  
  423. /* Ensure anything we have cached about the current page is refreshed on
  424. next use. */
  425. UpdateCache();
  426.  
  427. /* Ensure getPageVariable knows that the userloc needs updating next time it runs. */
  428. priv_userlocIsStale = true;
  429.  
  430. /* Run the function to do whatever we want. */
  431. callbackFunction();
  432.  
  433. /* Initialise a new observer for subsequent nav clicks. */
  434. var newTarget = priv_document.getElementById('apsleft');
  435. if(newTarget)
  436. {
  437. PRObserver.observe(newTarget, priv_mutationConfig);
  438. }
  439. else
  440. {
  441. this.DebugLog("(FATAL:priv_addPRCallback) Unable to attach to AP Counter body for Partial Refresh detection.", this.e_logLevel.QUIET);
  442. }
  443. });
  444.  
  445. PRObserver.observe(mutationTarget, priv_mutationConfig);
  446. }
  447. else
  448. {
  449. this.DebugLog("(FATAL:priv_addPRCallback) Unable to attach to AP Counter body for Partial Refresh detection.", this.e_logLevel.QUIET);
  450. }
  451. };
  452.  
  453. /*
  454. * Configuration Class Functions
  455. */
  456. this.AddConfiguration = function(arrConfigItems, eAttachment, sTitle, fSaveCallback, oConfigObject)
  457. {
  458. if (!this.PageIs(this.e_page.OPTIONS))
  459. {
  460. this.DebugLog("(FATAL:AddConfiguration) Configuration can only be called from " + this.e_page.OPTIONS, this.e_logLevel.QUIET);
  461. return;
  462. }
  463.  
  464. var configDivRows = [];
  465.  
  466. if (oConfigObject !== null)
  467. {
  468. if ((priv_configObject === null) || (priv_configObject === oConfigObject))
  469. {
  470. priv_configObject = oConfigObject;
  471. }
  472. else
  473. {
  474. this.DebugLog("(WARNING:AddConfiguration) Attempted to add a new configuration object, you must use the same object for all configuration forms!", this.e_logLevel.QUIET);
  475. }
  476. }
  477. else
  478. {
  479. this.DebugLog("(WARNING:AddConfiguration) oConfigObject is null", this.e_logLevel.QUIET);
  480. }
  481.  
  482. for (var i = 0; i < arrConfigItems.length; i++)
  483. {
  484. var div = null;
  485. var key = null;
  486. var description = null;
  487. var initialValue = null;
  488. var arrOptions = null;
  489. var arrOptionStrings = null;
  490.  
  491. if (arrConfigItems[i].length >= 1)
  492. {
  493. description = arrConfigItems[i][0];
  494. }
  495. if (arrConfigItems[i].length >= 2)
  496. {
  497. key = arrConfigItems[i][1];
  498. initialValue = priv_GetProperty(key, priv_configObject);
  499. priv_configValues.push(key);
  500. }
  501. if (arrConfigItems[i].length >= 3)
  502. {
  503. arrOptions = arrConfigItems[i][2];
  504. if (arrConfigItems[i].length >= 4)
  505. arrOptionStrings = arrConfigItems[i][3];
  506. }
  507.  
  508. // This is not a text or spacer object.
  509. if (initialValue !== null)
  510. {
  511. if (description === null)
  512. {
  513. this.DebugLog("FATAL:AddConfiguration) Description value for config object at index " + i + " must be set.");
  514. return;
  515. }
  516.  
  517. // if arrOptions is set assume select box
  518. if (arrOptions !== null)
  519. {
  520. div = priv_CreateSelectBox(key, description, initialValue, arrOptions, arrOptionStrings);
  521. }
  522. else
  523. {
  524. switch (typeof initialValue)
  525. {
  526. case "boolean":
  527. div = priv_CreateCheckBox(key, description, initialValue);
  528. break;
  529.  
  530. case "number":
  531. // Don't let this fall through to the "string" case because we don't want to confuse
  532. // char and number handling, and it's perfectly valid to have a 1-digit number.
  533. div = priv_CreateTextBox(key, description, initialValue, true);
  534. break;
  535.  
  536. case "string":
  537. if (initialValue.length === 1)
  538. {
  539. div = priv_CreateCharBox(key, description, initialValue, false);
  540. }
  541. else
  542. {
  543. div = priv_CreateTextBox(key, description, initialValue, false);
  544. }
  545. break;
  546.  
  547. default:
  548. this.DebugLog("(WARNING:AddConfiguration) Unsupported type " + typeof initialValue + " at index " + i);
  549. continue;
  550. }
  551. }
  552. }
  553. else if (description)
  554. {
  555. div = priv_CreateText(description);
  556. }
  557. else
  558. {
  559. div = priv_CreateSpacer();
  560. }
  561.  
  562. if (div !== null)
  563. {
  564. configDivRows.push(div);
  565. }
  566. }
  567.  
  568. var attachPoint = null;
  569. var configDiv = priv_GetConfigDiv(sTitle);
  570. var saveButton = priv_document.createElement('input');
  571.  
  572. /* Hold on to the callback, it will be called after priv_SaveConfig has modified the original object vlaues. */
  573. if (fSaveCallback !== null)
  574. {
  575. priv_configCallback = fSaveCallback;
  576. }
  577.  
  578. saveButton.type = "submit";
  579. saveButton.value = "Save";
  580. saveButton.addEventListener('click', priv_SaveConfig);
  581.  
  582. var auntieScriptWorkAround = priv_document.getElementById('Table_Options');
  583. if (auntieScriptWorkAround !== null)
  584. {
  585. if (eAttachment === this.e_configAttach.RIGHT)
  586. {
  587. attachPoint = auntieScriptWorkAround.getElementsByTagName('table')[0].rows[0].cells[2];
  588. }
  589. else
  590. {
  591. attachPoint = auntieScriptWorkAround.getElementsByTagName('table')[0].rows[0].cells[0];
  592. }
  593. }
  594. else
  595. {
  596. if (eAttachment === this.e_configAttach.RIGHT)
  597. {
  598. attachPoint = priv_document.getElementsByTagName('table')[3].rows[0].cells[2];
  599. }
  600. else
  601. {
  602. attachPoint = priv_document.getElementsByTagName('table')[3].rows[0].cells[0];
  603. }
  604. }
  605.  
  606. configDiv.appendChild(priv_CreateSpacer());
  607.  
  608. for (var i = 0; i < configDivRows.length; i++)
  609. {
  610. configDiv.appendChild(configDivRows[i]);
  611. }
  612.  
  613. configDiv.appendChild(priv_CreateSpacer());
  614. configDiv.appendChild(saveButton);
  615.  
  616. attachPoint.appendChild(priv_CreateSpacer());
  617. attachPoint.appendChild(priv_CreateSpacer());
  618. attachPoint.appendChild(configDiv);
  619. };
  620.  
  621. this.GetConfigElement = function(sKey)
  622. {
  623. var unique_id = priv_uniqid + sKey;
  624. return priv_document.getElementById(unique_id);
  625. };
  626.  
  627.  
  628. /* **************************
  629. * Private Functions
  630. * ************************* */
  631. // Returns true if the key is valid, otherwise false.
  632. priv_IsScriptKeyValid = function(key)
  633. {
  634. if (key.length !== 16)
  635. {
  636. this.DebugLog("(FATAL:Constructor) scriptKey parameter must be 16 characters long.", this.e_logLevel.QUIET);
  637. return false;
  638. }
  639. if (key.slice(0,3) !== "PAL")
  640. {
  641. this.DebugLog("(FATAL:Constructor) scriptKey parameter must start with 'PAL'.", this.e_logLevel.QUIET);
  642. return false;
  643. }
  644. if (!(/^[0-9A-F]+$/i.test(key.slice(3, key.length))))
  645. {
  646. this.DebugLog("(FATAL) scriptKey parameter must be the word 'PAL' followed by 13 hexadecimal characters.", this.e_logLevel.QUIET);
  647. return false;
  648. }
  649. return true;
  650. };
  651.  
  652. /*
  653. * Functions for Toast Notifications
  654. */
  655. priv_InitialiseToasts = function()
  656. {
  657. var attach = priv_GetToastAttachPoint();
  658. var styleTag = attach.createElement('style');
  659. styleTag.type = "text/css";
  660. styleTag.media = "screen";
  661.  
  662. styleTag.innerHTML = "#" + priv_uniqid + "pardusMonkeyToast { cursor: pointer; border-color: #DDD; border-style: solid; border-radius: 2px; color: #222; width: 50%; margin: 0 auto; position: fixed; z-index: 101; top: 0; left: 0; right: 0; background: " + this.e_toastStyle.SUCCESS + ";text-align: center; line-height: 2.5; overflow: hidden; -webkit-box-shadow: 0 0 5px black; -moz-box-shadow: 0 0 5px black; box-shadow: 0 0 5px black; display: none;} ";
  663.  
  664. var toastDiv = attach.createElement('div');
  665. toastDiv.id = priv_toastName;
  666. toastDiv.innerHTML = this.g_scriptName + ': No messages available.';
  667. toastDiv.addEventListener('click', priv_CloseToast, false);
  668.  
  669. attach.head.appendChild(styleTag);
  670. attach.body.appendChild(toastDiv);
  671. };
  672.  
  673. priv_CloseToast = function()
  674. {
  675. var attach = priv_GetToastAttachPoint();
  676. var toastDiv = attach.getElementById(priv_toastName);
  677. var timeNow = GetPageVariable(e_pageVar.MILLI_TIME, attach);
  678.  
  679. if (timeNow !== priv_toastMilliTime)
  680. {
  681. // If the frame was reloaded while we were showing the Toast, show it again in case the user
  682. // didn't have time to read it.
  683. Toast(priv_currentToast.text, priv_currentToast.style);
  684. }
  685. else
  686. {
  687. if(toastDiv)
  688. {
  689. toastDiv.style.display = "none";
  690. }
  691. else
  692. {
  693. console.log("[PardusMonkey]: (FATAL:priv_CloseToast) Unable to locate notification div with ID: " + priv_toastName);
  694. }
  695. }
  696. };
  697.  
  698. priv_GetToastAttachPoint = function()
  699. {
  700. // window.content.frames doesn't work in PR mode, window object is hella messed up. Damn AJAX. :o
  701. if (this.PREnabled())
  702. return priv_document;
  703.  
  704. try
  705. {
  706. return window.content.frames.msgframe.document;
  707. }
  708. catch (e){}
  709.  
  710. return priv_document;
  711. };
  712.  
  713. priv_IsNumber = function(n)
  714. {
  715. n = n.replace(/,/,".");
  716. return (!isNaN(parseFloat(n)) && isFinite(n));
  717. };
  718.  
  719. /*
  720. * Private Partial Refresh Functions
  721. */
  722. function priv_UpdateUserLoc()
  723. {
  724. var scripts = document.getElementsByTagName('head')[0].getElementsByTagName('script');
  725.  
  726. for (var i = 0; i < scripts.length; i++)
  727. {
  728. if(!scripts[i].src)
  729. {
  730. if(scripts[i].textContent.indexOf("userloc") !== -1)
  731. {
  732. scripts[i].textContent = scripts[i].textContent.replace(/userloc = (\d+);/, "userloc = " + userloc + ";");
  733. break;
  734. }
  735. }
  736. }
  737. }
  738.  
  739. /*
  740. * Private Config Functions
  741. */
  742. priv_CreateSpacer = function()
  743. {
  744. return priv_document.createElement('br');
  745. };
  746.  
  747. priv_CreateCheckBox = function(sKey, sDescription, bInitialValue)
  748. {
  749. var tr = priv_document.createElement('tr');
  750. var input = priv_document.createElement('input');
  751. var label = priv_document.createElement('label');
  752. var unique_id = priv_uniqid + sKey;
  753.  
  754. input.id = unique_id;
  755. input.type = "checkbox";
  756. input.value = unique_id;
  757. label.setAttribute('for', unique_id);
  758. label.textContent = sDescription;
  759.  
  760. input.checked = bInitialValue;
  761.  
  762. tr.appendChild(input);
  763. tr.appendChild(label);
  764. return tr;
  765. };
  766.  
  767. priv_CreateTextBox = function(sKey, sDescription, sInitialValue, bNumber)
  768. {
  769. var div = priv_CreateDiv();
  770. var input = priv_CreateInput();
  771. var label = priv_document.createElement('label');
  772. var unique_id = priv_uniqid + sKey;
  773.  
  774. label.setAttribute('for', unique_id);
  775. label.textContent = sDescription;
  776.  
  777. input.id = unique_id;
  778. input.type = "text";
  779. input.style.width = this.g_configInputWidth;
  780. if (sInitialValue) input.value = sInitialValue;
  781. if (bNumber === true) input.className = "number";
  782.  
  783. div.appendChild(label);
  784. div.appendChild(input);
  785. return div;
  786. };
  787.  
  788. priv_CreateSelectBox = function(sKey, sDescription, nInitialValue, arrData, arrDataStrings)
  789. {
  790. var div = priv_CreateDiv();
  791. var select = priv_document.createElement('select');
  792. var label = priv_document.createElement('label');
  793. var unique_id = priv_uniqid + sKey;
  794. var option;
  795.  
  796. label.setAttribute('for', unique_id);
  797. label.textContent = sDescription;
  798.  
  799. select.id = unique_id;
  800. select.style.width = this.g_configInputWidth;
  801.  
  802. for (var i = 0; i < arrData.length; i++)
  803. {
  804. option = priv_document.createElement('option');
  805. if ((arrDataStrings == null) || (arrDataStrings.length != arrData.length))
  806. option.textContent = arrData[i];
  807. else
  808. option.textContent = arrDataStrings[i];
  809. option.value = arrData[i];
  810. select.appendChild(option);
  811. }
  812.  
  813. if (nInitialValue) select.value = nInitialValue;
  814.  
  815. div.appendChild(label);
  816. div.appendChild(select);
  817. return div;
  818. };
  819.  
  820. priv_CreateCharBox = function(sKey, sDescription, cInitialValue)
  821. {
  822. var div = priv_CreateDiv();
  823. var span = priv_CreateSpan();
  824. var tb = priv_CreateInput();
  825. var unique_id = priv_uniqid + sKey;
  826.  
  827. span.innerHTML = sDescription;
  828. span.style.marginLeft = "5px";
  829. tb.type = "text";
  830. tb.id = unique_id;
  831. tb.maxLength = "1";
  832. tb.style.width = "25px";
  833. tb.style.textAlign = "center";
  834. tb.setAttribute("onclick","this.focus(); this.select();");
  835.  
  836. if (cInitialValue) tb.value = cInitialValue;
  837.  
  838. div.style.marginBottom = "4px";
  839.  
  840. div.appendChild(tb);
  841. div.appendChild(span);
  842. return div;
  843. };
  844.  
  845. priv_CreateText = function(sText)
  846. {
  847. var div = priv_CreateDiv();
  848.  
  849. div.innerHTML = sText;
  850.  
  851. return div;
  852. };
  853.  
  854. priv_GetConfigDiv = function(sTitle)
  855. {
  856. var div = priv_CreateDiv();
  857. var headerDiv = priv_CreateDiv();
  858.  
  859. div.width = "100%";
  860. div.style.padding = "5px";
  861. div.style.backgroundImage = "url(http://static.pardus.at/img/stdhq/bgd.gif)";
  862.  
  863. headerDiv.innerHTML = "<b>" + this.g_scriptName + " - " + sTitle + "</b>";
  864. headerDiv.setAttribute('style', 'background:none repeat scroll 0 0 #600; color:#CCC; padding: 4px; margin: -5px; text-align: center;');
  865.  
  866. div.appendChild(headerDiv);
  867.  
  868. return div;
  869. };
  870.  
  871. /* Creating new elements is slower than cloning, so cache the element we create and just return a clone of it. */
  872. priv_CreateDiv = function()
  873. {
  874. if (priv_div === null)
  875. {
  876. priv_div = priv_document.createElement('div');
  877. }
  878.  
  879. return priv_div.cloneNode(true);
  880. };
  881.  
  882. priv_CreateInput = function()
  883. {
  884. if (priv_input === null)
  885. {
  886. priv_input = priv_document.createElement('input');
  887. }
  888.  
  889. return priv_input.cloneNode(true);
  890. };
  891.  
  892. priv_CreateParagraph = function()
  893. {
  894. if (priv_p === null)
  895. {
  896. priv_p = priv_document.createElement('p');
  897. }
  898.  
  899. return priv_p.cloneNode(true);
  900. };
  901.  
  902. priv_CreateScript = function()
  903. {
  904. if (priv_script === null)
  905. {
  906. priv_script = priv_document.createElement('script');
  907. }
  908.  
  909. return priv_script.cloneNode(true);
  910. };
  911.  
  912. priv_CreateSpan = function()
  913. {
  914. if (priv_span === null)
  915. {
  916. priv_span = priv_document.createElement('span');
  917. }
  918.  
  919. return priv_span.cloneNode(true);
  920. };
  921.  
  922. priv_SaveConfig = function()
  923. {
  924. for (var i = 0; i < priv_configValues.length; i++)
  925. {
  926. var key = priv_configValues[i];
  927. var item = priv_document.getElementById(priv_uniqid + key);
  928.  
  929. if (item)
  930. {
  931. switch (typeof priv_GetProperty(key, priv_configObject))
  932. {
  933. case "boolean":
  934. priv_SetProperty(key, priv_configObject, item.checked);
  935. break;
  936. case "string":
  937. if ((item.className === "number") && (!priv_IsNumber(item.value)))
  938. {
  939. Toast("Value for " + key + " must be numeric", e_toastStyle.ERROR);
  940. return;
  941. }
  942. else
  943. {
  944. priv_SetProperty(key, priv_configObject, item.value);
  945. }
  946. break;
  947. case "number":
  948. priv_SetProperty(key, priv_configObject, item.value);
  949. break;
  950. default:
  951. DebugLog("Unable to determine property type: " + key + ":" + typeof priv_GetProperty(key, priv_configObject), e_logLevel.QUIET);
  952. break;
  953. }
  954. }
  955. else
  956. {
  957. DebugLog("(FATAL:priv_SaveConfig) Unable to locate element for ID: " + priv_uniqid + key, e_logLevel.QUIET);
  958. }
  959. }
  960.  
  961. /* Allow the author to save whatever we just changed. */
  962. if (priv_configCallback instanceof Function)
  963. {
  964. priv_configCallback();
  965. }
  966. else
  967. {
  968. DebugLog("(FATAL:priv_SaveConfig) user defined callback function is not a function!", e_logLevel.QUIET);
  969. }
  970. };
  971.  
  972. priv_GetProperty = function(propName, object)
  973. {
  974. return object[propName];
  975. };
  976.  
  977. priv_SetProperty = function(propName, object, value)
  978. {
  979. object[propName] = value;
  980. };
  981.  
  982. /* **************************
  983. * PardusMonkey Constructor
  984. * ************************* */
  985. // Ensure the key is a valid PAL key before returning a valid object.
  986. if (!priv_IsScriptKeyValid(scriptKey))
  987. {
  988. return null;
  989. }
  990.  
  991. return this;
  992. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址