GM_config CN

sizzlemctwizzle 的 GM_config 库中文版本

目前为 2014-10-30 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/6158/23267/GM_config%20CN.js

  1. // ==UserScript==
  2. // @name GM_config CN
  3. // @namespace https://github.com/ywzhaiqi/
  4. // @description sizzlemctwizzle 的 GM_config 库中文版本
  5. // @source https://github.com/ywzhaiqi/GM_config
  6. // @version 1.0
  7.  
  8. // @copyright sizzlemctwizzle
  9.  
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_deleteValue
  13. // @license LGPL 3
  14. // ==/UserScript==
  15.  
  16. /*
  17. 修改说明
  18. 1、改成中文 "确定"、"取消" 按钮。
  19. 2、select 新增了 textContents 数组。
  20. */
  21.  
  22. /*
  23. Copyright 2009+, GM_config Contributors (https://github.com/sizzlemctwizzle/GM_config)
  24.  
  25. GM_config Contributors:
  26. Mike Medley <medleymind@gmail.com>
  27. Joe Simmons
  28. Izzy Soft
  29. Marti Martz
  30.  
  31. GM_config is distributed under the terms of the GNU Lesser General Public License.
  32.  
  33. GM_config is free software: you can redistribute it and/or modify
  34. it under the terms of the GNU Lesser General Public License as published by
  35. the Free Software Foundation, either version 3 of the License, or
  36. (at your option) any later version.
  37.  
  38. This program is distributed in the hope that it will be useful,
  39. but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. GNU Lesser General Public License for more details.
  42.  
  43. You should have received a copy of the GNU Lesser General Public License
  44. along with this program. If not, see <http://www.gnu.org/licenses/>.
  45. */
  46.  
  47. // The GM_config constructor
  48. function GM_configStruct() {
  49. // call init() if settings were passed to constructor
  50. if (arguments.length) {
  51. GM_configInit(this, arguments);
  52. this.onInit();
  53. }
  54. }
  55.  
  56. // This is the initializer function
  57. function GM_configInit(config, args) {
  58. // Initialize instance variables
  59. if (typeof config.fields == "undefined") {
  60. config.fields = {};
  61. config.onInit = config.onInit || function() {};
  62. config.onOpen = config.onOpen || function() {};
  63. config.onSave = config.onSave || function() {};
  64. config.onClose = config.onClose || function() {};
  65. config.onReset = config.onReset || function() {};
  66. config.isOpen = false;
  67. config.title = 'User Script Settings';
  68. config.css = {
  69. basic: [
  70. "#GM_config * { font-family: arial,tahoma,myriad pro,sans-serif; }",
  71. "#GM_config { background: #FFF; }",
  72. "#GM_config input[type='radio'] { margin-right: 8px; }",
  73. "#GM_config .indent40 { margin-left: 40%; }",
  74. "#GM_config .field_label { font-size: 12px; font-weight: bold; margin-right: 6px; }",
  75. "#GM_config .radio_label { font-size: 12px; }",
  76. "#GM_config .block { display: block; }",
  77. "#GM_config .saveclose_buttons { margin: 16px 10px 10px; padding: 2px 12px; }",
  78. "#GM_config .reset, #GM_config .reset a," +
  79. " #GM_config_buttons_holder { color: #000; text-align: right; }",
  80. "#GM_config .config_header { font-size: 20pt; margin: 0; }",
  81. "#GM_config .config_desc, #GM_config .section_desc, #GM_config .reset { font-size: 9pt; }",
  82. "#GM_config .center { text-align: center; }",
  83. "#GM_config .section_header_holder { margin-top: 8px; }",
  84. "#GM_config .config_var { margin: 0 0 4px; }",
  85. "#GM_config .section_header { background: #414141; border: 1px solid #000; color: #FFF;",
  86. " font-size: 13pt; margin: 0; }",
  87. "#GM_config .section_desc { background: #EFEFEF; border: 1px solid #CCC; color: #575757;" +
  88. " font-size: 9pt; margin: 0 0 6px; }"
  89. ].join('\n') + '\n',
  90. basicPrefix: "GM_config",
  91. stylish: ""
  92. };
  93. }
  94.  
  95. if (args.length == 1 &&
  96. typeof args[0].id == "string" &&
  97. typeof args[0].appendChild != "function") var settings = args[0];
  98. else {
  99. // Provide backwards-compatibility with argument style intialization
  100. var settings = {};
  101.  
  102. // loop through GM_config.init() arguments
  103. for (var i = 0, l = args.length, arg; i < l; ++i) {
  104. arg = args[i];
  105.  
  106. // An element to use as the config window
  107. if (typeof arg.appendChild == "function") {
  108. settings.frame = arg;
  109. continue;
  110. }
  111.  
  112. switch (typeof arg) {
  113. case 'object':
  114. for (var j in arg) { // could be a callback functions or settings object
  115. if (typeof arg[j] != "function") { // we are in the settings object
  116. settings.fields = arg; // store settings object
  117. break; // leave the loop
  118. } // otherwise it must be a callback function
  119. if (!settings.events) settings.events = {};
  120. settings.events[j] = arg[j];
  121. }
  122. break;
  123. case 'function': // passing a bare function is set to open callback
  124. settings.events = {onOpen: arg};
  125. break;
  126. case 'string': // could be custom CSS or the title string
  127. if (/\w+\s*\{\s*\w+\s*:\s*\w+[\s|\S]*\}/.test(arg))
  128. settings.css = arg;
  129. else
  130. settings.title = arg;
  131. break;
  132. }
  133. }
  134. }
  135.  
  136. /* Initialize everything using the new settings object */
  137. // Set the id
  138. if (settings.id) config.id = settings.id;
  139. else if (typeof config.id == "undefined") config.id = 'GM_config';
  140.  
  141. // Set the title
  142. if (settings.title) config.title = settings.title;
  143.  
  144. // Set the custom css
  145. if (settings.css) config.css.stylish = settings.css;
  146.  
  147. // Set the frame
  148. if (settings.frame) config.frame = settings.frame;
  149.  
  150. // Set the event callbacks
  151. if (settings.events) {
  152. var events = settings.events;
  153. for (var e in events)
  154. config["on" + e.charAt(0).toUpperCase() + e.slice(1)] = events[e];
  155. }
  156.  
  157. // Create the fields
  158. if (settings.fields) {
  159. var stored = config.read(), // read the stored settings
  160. fields = settings.fields,
  161. customTypes = settings.types || {};
  162.  
  163. for (var id in fields) {
  164. var field = fields[id];
  165.  
  166. // for each field definition create a field object
  167. if (field)
  168. config.fields[id] = new GM_configField(field, stored[id], id,
  169. customTypes[field.type]);
  170. else if (config.fields[id]) delete config.fields[id];
  171. }
  172. }
  173.  
  174. // If the id has changed we must modify the default style
  175. if (config.id != config.css.basicPrefix) {
  176. config.css.basic = config.css.basic.replace(
  177. new RegExp('#' + config.css.basicPrefix, 'gm'), '#' + config.id);
  178. config.css.basicPrefix = config.id;
  179. }
  180. }
  181.  
  182. GM_configStruct.prototype = {
  183. // Support old method of initalizing
  184. init: function() {
  185. GM_configInit(this, arguments);
  186. this.onInit();
  187. },
  188.  
  189. // call GM_config.open() from your script to open the menu
  190. open: function () {
  191. // Die if the menu is already open on this page
  192. // You can have multiple instances but you can't open the same instance twice
  193. var match = document.getElementById(this.id);
  194. if (match && (match.tagName == "IFRAME" || match.childNodes.length > 0)) return;
  195.  
  196. // Sometimes "this" gets overwritten so create an alias
  197. var config = this;
  198.  
  199. // Function to build the mighty config window :)
  200. function buildConfigWin (body, head) {
  201. var create = config.create,
  202. fields = config.fields,
  203. configId = config.id,
  204. bodyWrapper = create('div', {id: configId + '_wrapper'});
  205.  
  206. // Append the style which is our default style plus the user style
  207. head.appendChild(
  208. create('style', {
  209. type: 'text/css',
  210. textContent: config.css.basic + config.css.stylish
  211. }));
  212.  
  213. // Add header and title
  214. bodyWrapper.appendChild(create('div', {
  215. id: configId + '_header',
  216. className: 'config_header block center'
  217. }, config.title));
  218.  
  219. // Append elements
  220. var section = bodyWrapper,
  221. secNum = 0; // Section count
  222.  
  223. // loop through fields
  224. for (var id in fields) {
  225. var field = fields[id],
  226. settings = field.settings;
  227.  
  228. if (settings.section) { // the start of a new section
  229. section = bodyWrapper.appendChild(create('div', {
  230. className: 'section_header_holder',
  231. id: configId + '_section_' + secNum
  232. }));
  233.  
  234. if (Object.prototype.toString.call(settings.section) !== '[object Array]')
  235. settings.section = [settings.section];
  236.  
  237. if (settings.section[0])
  238. section.appendChild(create('div', {
  239. className: 'section_header center',
  240. id: configId + '_section_header_' + secNum
  241. }, settings.section[0]));
  242.  
  243. if (settings.section[1])
  244. section.appendChild(create('p', {
  245. className: 'section_desc center',
  246. id: configId + '_section_desc_' + secNum
  247. }, settings.section[1]));
  248. ++secNum;
  249. }
  250.  
  251. // Create field elements and append to current section
  252. section.appendChild((field.wrapper = field.toNode(configId)));
  253. }
  254.  
  255. // Add save and close buttons
  256. bodyWrapper.appendChild(create('div',
  257. {id: configId + '_buttons_holder'},
  258.  
  259. create('button', {
  260. id: configId + '_saveBtn',
  261. textContent: '确定',
  262. title: '部分选项需要刷新页面才能生效',
  263. className: 'saveclose_buttons',
  264. onclick: function () {
  265. config.save();
  266. config.close();
  267. }
  268. }),
  269.  
  270. create('button', {
  271. id: configId + '_closeBtn',
  272. textContent: '取消',
  273. title: '取消本次设置,所有选项还原',
  274. className: 'saveclose_buttons',
  275. onclick: function () {
  276. config.close()
  277. }
  278. }),
  279.  
  280. create('div',
  281. {className: 'reset_holder block'},
  282.  
  283. // Reset link
  284. create('a', {
  285. id: configId + '_resetLink',
  286. textContent: '恢复默认设置',
  287. href: '#',
  288. title: '恢复所有设置的内容为默认值',
  289. className: 'reset',
  290. onclick: function (e) {
  291. e.preventDefault();
  292. config.reset()
  293. }
  294. })
  295. )));
  296.  
  297. body.appendChild(bodyWrapper); // Paint everything to window at once
  298. config.center(); // Show and center iframe
  299. window.addEventListener('resize', config.center, false); // Center frame on resize
  300.  
  301. // Call the open() callback function
  302. config.onOpen(config.frame.contentDocument || config.frame.ownerDocument,
  303. config.frame.contentWindow || window,
  304. config.frame);
  305.  
  306. // Close frame on window close
  307. window.addEventListener('beforeunload', function () {
  308. config.close();
  309. }, false);
  310.  
  311. // Now that everything is loaded, make it visible
  312. config.frame.style.display = "block";
  313. config.isOpen = true;
  314. }
  315.  
  316. // Change this in the onOpen callback using this.frame.setAttribute('style', '')
  317. var defaultStyle = 'bottom: auto; border: 1px solid #000; display: none; height: 75%;'
  318. + ' left: 0; margin: 0; max-height: 95%; max-width: 95%; opacity: 0;'
  319. + ' overflow: auto; padding: 0; position: fixed; right: auto; top: 0;'
  320. + ' width: 75%; z-index: 999999999;';
  321.  
  322. // Either use the element passed to init() or create an iframe
  323. if (this.frame) {
  324. this.frame.id = this.id; // Allows for prefixing styles with the config id
  325. this.frame.setAttribute('style', defaultStyle);
  326. buildConfigWin(this.frame, this.frame.ownerDocument.getElementsByTagName('head')[0]);
  327. } else {
  328. // Create frame
  329. document.body.appendChild((this.frame = this.create('iframe', {
  330. id: this.id,
  331. style: defaultStyle
  332. })));
  333.  
  334. // In WebKit src can't be set until it is added to the page
  335. this.frame.src = 'about:blank';
  336. // we wait for the iframe to load before we can modify it
  337. this.frame.addEventListener('load', function(e) {
  338. var frame = config.frame;
  339. var body = frame.contentDocument.getElementsByTagName('body')[0];
  340. body.id = config.id; // Allows for prefixing styles with the config id
  341. buildConfigWin(body, frame.contentDocument.getElementsByTagName('head')[0]);
  342. }, false);
  343. }
  344. },
  345.  
  346. save: function () {
  347. var forgotten = this.write();
  348. this.onSave(forgotten); // Call the save() callback function
  349. },
  350.  
  351. close: function() {
  352. if (!this.frame) return;
  353. // If frame is an iframe then remove it
  354. if (this.frame.contentDocument) {
  355. this.remove(this.frame);
  356. this.frame = null;
  357. } else { // else wipe its content
  358. this.frame.innerHTML = "";
  359. this.frame.style.display = "none";
  360. }
  361.  
  362. // Null out all the fields so we don't leak memory
  363. var fields = this.fields;
  364. for (var id in fields) {
  365. var field = fields[id];
  366. field.wrapper = null;
  367. field.node = null;
  368. }
  369.  
  370. this.onClose(); // Call the close() callback function
  371. this.isOpen = false;
  372. },
  373.  
  374. set: function (name, val) {
  375. this.fields[name].value = val;
  376. },
  377.  
  378. get: function (name) {
  379. return this.fields[name].value;
  380. },
  381.  
  382. write: function (store, obj) {
  383. if (!obj) {
  384. var values = {},
  385. forgotten = {},
  386. fields = this.fields;
  387.  
  388. for (var id in fields) {
  389. var field = fields[id];
  390. var value = field.toValue();
  391.  
  392. if (field.save) {
  393. if (value != null) {
  394. values[id] = value;
  395. field.value = value;
  396. } else
  397. values[id] = field.value;
  398. } else
  399. forgotten[id] = value;
  400. }
  401. }
  402. try {
  403. this.setValue(store || this.id, this.stringify(obj || values));
  404. } catch(e) {
  405. this.log("GM_config failed to save settings!");
  406. }
  407.  
  408. return forgotten;
  409. },
  410.  
  411. read: function (store) {
  412. try {
  413. var rval = this.parser(this.getValue(store || this.id, '{}'));
  414. } catch(e) {
  415. this.log("GM_config failed to read saved settings!");
  416. var rval = {};
  417. }
  418. return rval;
  419. },
  420.  
  421. reset: function () {
  422. var fields = this.fields;
  423.  
  424. // Reset all the fields
  425. for (var id in fields) fields[id].reset();
  426.  
  427. this.onReset(); // Call the reset() callback function
  428. },
  429.  
  430. create: function () {
  431. switch(arguments.length) {
  432. case 1:
  433. var A = document.createTextNode(arguments[0]);
  434. break;
  435. default:
  436. var A = document.createElement(arguments[0]),
  437. B = arguments[1];
  438. for (var b in B) {
  439. if (b.indexOf("on") == 0)
  440. A.addEventListener(b.substring(2), B[b], false);
  441. else if (",style,accesskey,id,name,src,href,which,for".indexOf("," +
  442. b.toLowerCase()) != -1)
  443. A.setAttribute(b, B[b]);
  444. else
  445. A[b] = B[b];
  446. }
  447. if (typeof arguments[2] == "string")
  448. A.innerHTML = arguments[2];
  449. else
  450. for (var i = 2, len = arguments.length; i < len; ++i)
  451. A.appendChild(arguments[i]);
  452. }
  453. return A;
  454. },
  455.  
  456. center: function () {
  457. var node = this.frame;
  458. if (!node) return;
  459. var style = node.style,
  460. beforeOpacity = style.opacity;
  461. if (style.display == 'none') style.opacity = '0';
  462. style.display = '';
  463. style.top = Math.floor((window.innerHeight / 2) - (node.offsetHeight / 2)) + 'px';
  464. style.left = Math.floor((window.innerWidth / 2) - (node.offsetWidth / 2)) + 'px';
  465. style.opacity = '1';
  466. },
  467.  
  468. remove: function (el) {
  469. if (el && el.parentNode) el.parentNode.removeChild(el);
  470. }
  471. };
  472.  
  473. // Define a bunch of API stuff
  474. (function() {
  475. var isGM = typeof GM_getValue != 'undefined' &&
  476. typeof GM_getValue('a', 'b') != 'undefined',
  477. setValue, getValue, stringify, parser;
  478.  
  479. // Define value storing and reading API
  480. if (!isGM) {
  481. setValue = function (name, value) {
  482. return localStorage.setItem(name, value);
  483. };
  484. getValue = function(name, def){
  485. var s = localStorage.getItem(name);
  486. return s == null ? def : s
  487. };
  488.  
  489. // We only support JSON parser outside GM
  490. stringify = JSON.stringify;
  491. parser = JSON.parse;
  492. } else {
  493. setValue = GM_setValue;
  494. getValue = GM_getValue;
  495. stringify = typeof JSON == "undefined" ?
  496. function(obj) {
  497. return obj.toSource();
  498. } : JSON.stringify;
  499. parser = typeof JSON == "undefined" ?
  500. function(jsonData) {
  501. return (new Function('return ' + jsonData + ';'))();
  502. } : JSON.parse;
  503. }
  504.  
  505. GM_configStruct.prototype.isGM = isGM;
  506. GM_configStruct.prototype.setValue = setValue;
  507. GM_configStruct.prototype.getValue = getValue;
  508. GM_configStruct.prototype.stringify = stringify;
  509. GM_configStruct.prototype.parser = parser;
  510. GM_configStruct.prototype.log = window.console ?
  511. console.log : (isGM && typeof GM_log != 'undefined' ?
  512. GM_log : (window.opera ?
  513. opera.postError : function(){ /* no logging */ }
  514. ));
  515. })();
  516.  
  517. function GM_configDefaultValue(type, options) {
  518. var value;
  519.  
  520. if (type.indexOf('unsigned ') == 0)
  521. type = type.substring(9);
  522.  
  523. switch (type) {
  524. case 'radio': case 'select':
  525. value = options[0];
  526. break;
  527. case 'checkbox':
  528. value = false;
  529. break;
  530. case 'int': case 'integer':
  531. case 'float': case 'number':
  532. value = 0;
  533. break;
  534. default:
  535. value = '';
  536. }
  537.  
  538. return value;
  539. }
  540.  
  541. function GM_configField(settings, stored, id, customType) {
  542. // Store the field's settings
  543. this.settings = settings;
  544. this.id = id;
  545. this.node = null;
  546. this.wrapper = null;
  547. this.save = typeof settings.save == "undefined" ? true : settings.save;
  548.  
  549. // Buttons are static and don't have a stored value
  550. if (settings.type == "button") this.save = false;
  551.  
  552. // if a default value wasn't passed through init() then
  553. // if the type is custom use its default value
  554. // else use default value for type
  555. // else use the default value passed through init()
  556. this['default'] = typeof settings['default'] == "undefined" ?
  557. customType ?
  558. customType['default']
  559. : GM_configDefaultValue(settings.type, settings.options)
  560. : settings['default'];
  561.  
  562. // Store the field's value
  563. this.value = typeof stored == "undefined" ? this['default'] : stored;
  564.  
  565. // Setup methods for a custom type
  566. if (customType) {
  567. this.toNode = customType.toNode;
  568. this.toValue = customType.toValue;
  569. this.reset = customType.reset;
  570. }
  571. }
  572.  
  573. GM_configField.prototype = {
  574. create: GM_configStruct.prototype.create,
  575.  
  576. toNode: function(configId) {
  577. var field = this.settings,
  578. value = this.value,
  579. options = field.options,
  580. type = field.type,
  581. id = this.id,
  582. labelPos = field.labelPos,
  583. create = this.create;
  584.  
  585. function addLabel(pos, labelEl, parentNode, beforeEl) {
  586. if (!beforeEl) beforeEl = parentNode.firstChild;
  587. switch (pos) {
  588. case 'right': case 'below':
  589. if (pos == 'below')
  590. parentNode.appendChild(create('br', {}));
  591. parentNode.appendChild(labelEl);
  592. break;
  593. default:
  594. if (pos == 'above')
  595. parentNode.insertBefore(create('br', {}), beforeEl);
  596. parentNode.insertBefore(labelEl, beforeEl);
  597. }
  598. }
  599.  
  600. var retNode = create('div', { className: 'config_var',
  601. id: configId + '_' + id + '_var',
  602. title: field.title || '' }),
  603. firstProp;
  604.  
  605. // Retrieve the first prop
  606. for (var i in field) { firstProp = i; break; }
  607.  
  608. var label = field.label && type != "button" ?
  609. create('label', {
  610. id: configId + '_' + id + '_field_label',
  611. for: configId + '_field_' + id,
  612. className: 'field_label'
  613. }, field.label) : null;
  614.  
  615. switch (type) {
  616. case 'textarea':
  617. retNode.appendChild((this.node = create('textarea', {
  618. innerHTML: value,
  619. id: configId + '_field_' + id,
  620. className: 'block',
  621. cols: (field.cols ? field.cols : 20),
  622. rows: (field.rows ? field.rows : 2)
  623. })));
  624. break;
  625. case 'radio':
  626. var wrap = create('div', {
  627. id: configId + '_field_' + id
  628. });
  629. this.node = wrap;
  630.  
  631. for (var i = 0, len = options.length; i < len; ++i) {
  632. var radLabel = create('label', {
  633. className: 'radio_label'
  634. }, options[i]);
  635.  
  636. var rad = wrap.appendChild(create('input', {
  637. value: options[i],
  638. type: 'radio',
  639. name: id,
  640. checked: options[i] == value
  641. }));
  642.  
  643. var radLabelPos = labelPos &&
  644. (labelPos == 'left' || labelPos == 'right') ?
  645. labelPos : firstProp == 'options' ? 'left' : 'right';
  646.  
  647. addLabel(radLabelPos, radLabel, wrap, rad);
  648. }
  649.  
  650. retNode.appendChild(wrap);
  651. break;
  652. case 'select':
  653. var wrap = create('select', {
  654. id: configId + '_field_' + id
  655. });
  656. this.node = wrap;
  657.  
  658. for (var i = 0, len = options.length; i < len; ++i) {
  659. var option = options[i];
  660. wrap.appendChild(create('option', {
  661. value: option,
  662. selected: option == value,
  663. }, field.textContents ? field.textContents[i] : option));
  664. }
  665.  
  666. retNode.appendChild(wrap);
  667. break;
  668. default: // fields using input elements
  669. var props = {
  670. id: configId + '_field_' + id,
  671. type: type,
  672. value: type == 'button' ? field.label : value
  673. };
  674.  
  675. switch (type) {
  676. case 'checkbox':
  677. props.checked = value;
  678. break;
  679. case 'button':
  680. props.size = field.size ? field.size : 25;
  681. if (field.script) field.click = field.script;
  682. if (field.click) props.onclick = field.click;
  683. break;
  684. case 'hidden':
  685. break;
  686. default:
  687. // type = text, int, or float
  688. props.type = 'text';
  689. props.size = field.size ? field.size : 25;
  690. }
  691.  
  692. retNode.appendChild((this.node = create('input', props)));
  693. }
  694.  
  695. if (label) {
  696. // If the label is passed first, insert it before the field
  697. // else insert it after
  698. if (!labelPos)
  699. labelPos = firstProp == "label" || type == "radio" ?
  700. "left" : "right";
  701.  
  702. addLabel(labelPos, label, retNode);
  703. }
  704.  
  705. return retNode;
  706. },
  707.  
  708. toValue: function() {
  709. var node = this.node,
  710. field = this.settings,
  711. type = field.type,
  712. unsigned = false,
  713. rval = null;
  714.  
  715. if (!node) return rval;
  716.  
  717. if (type.indexOf('unsigned ') == 0) {
  718. type = type.substring(9);
  719. unsigned = true;
  720. }
  721.  
  722. switch (type) {
  723. case 'checkbox':
  724. rval = node.checked;
  725. break;
  726. case 'select':
  727. rval = node[node.selectedIndex].value;
  728. break;
  729. case 'radio':
  730. var radios = node.getElementsByTagName('input');
  731. for (var i = 0, len = radios.length; i < len; ++i)
  732. if (radios[i].checked)
  733. rval = radios[i].value;
  734. break;
  735. case 'button':
  736. break;
  737. case 'int': case 'integer':
  738. case 'float': case 'number':
  739. var num = Number(node.value);
  740. var warn = 'Field labeled "' + field.label + '" expects a' +
  741. (unsigned ? ' positive ' : 'n ') + 'integer value';
  742.  
  743. if (isNaN(num) || (type.substr(0, 3) == 'int' &&
  744. Math.ceil(num) != Math.floor(num)) ||
  745. (unsigned && num < 0)) {
  746. alert(warn + '.');
  747. return null;
  748. }
  749.  
  750. if (!this._checkNumberRange(num, warn))
  751. return null;
  752. rval = num;
  753. break;
  754. default:
  755. rval = node.value;
  756. break;
  757. }
  758.  
  759. return rval; // value read successfully
  760. },
  761.  
  762. reset: function() {
  763. var node = this.node,
  764. field = this.settings,
  765. type = field.type;
  766.  
  767. if (!node) return;
  768.  
  769. switch (type) {
  770. case 'checkbox':
  771. node.checked = this['default'];
  772. break;
  773. case 'select':
  774. for (var i = 0, len = node.options.length; i < len; ++i)
  775. if (node.options[i].value == this['default'])
  776. node.selectedIndex = i;
  777. break;
  778. case 'radio':
  779. var radios = node.getElementsByTagName('input');
  780. for (var i = 0, len = radios.length; i < len; ++i)
  781. if (radios[i].value == this['default'])
  782. radios[i].checked = true;
  783. break;
  784. case 'button' :
  785. break;
  786. default:
  787. node.value = this['default'];
  788. break;
  789. }
  790. },
  791.  
  792. remove: function(el) {
  793. GM_configStruct.prototype.remove(el || this.wrapper);
  794. this.wrapper = null;
  795. this.node = null;
  796. },
  797.  
  798. reload: function() {
  799. var wrapper = this.wrapper;
  800. if (wrapper) {
  801. var fieldParent = wrapper.parentNode;
  802. fieldParent.insertBefore((this.wrapper = this.toNode()), wrapper);
  803. this.remove(wrapper);
  804. }
  805. },
  806.  
  807. _checkNumberRange: function(num, warn) {
  808. var field = this.settings;
  809. if (typeof field.min == "number" && num < field.min) {
  810. alert(warn + ' greater than or equal to ' + field.min + '.');
  811. return null;
  812. }
  813.  
  814. if (typeof field.max == "number" && num > field.max) {
  815. alert(warn + ' less than or equal to ' + field.max + '.');
  816. return null;
  817. }
  818. return true;
  819. }
  820. };
  821.  
  822. // Create default instance of GM_config
  823. var GM_config = new GM_configStruct();