XPath

xpath Function

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.cn-greasyfork.org/scripts/29174/189853/XPath.js

  1. // ==UserScript==
  2. // @name XPath
  3. // @namespace http://gm.wesley.eti.br/includes
  4. // @description xpath Function
  5. // @author w35l3y
  6. // @email w35l3y@brasnet.org
  7. // @copyright 2009+, w35l3y (http://gm.wesley.eti.br)
  8. // @license GNU GPL
  9. // @homepage http://gm.wesley.eti.br
  10. // @version 1.0.0.5
  11. // @language en
  12. // @include nowhere
  13. // ==/UserScript==
  14.  
  15. /**************************************************************************
  16.  
  17. Author 's NOTE
  18.  
  19. Original http://lowreal.net/blog/2007/11/17/1
  20.  
  21. ***************************************************************************
  22.  
  23. This program is free software: you can redistribute it and/or modify
  24. it under the terms of the GNU General Public License as published by
  25. the Free Software Foundation, either version 3 of the License, or
  26. (at your option) any later version.
  27.  
  28. This program is distributed in the hope that it will be useful,
  29. but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. GNU General Public License for more details.
  32.  
  33. You should have received a copy of the GNU General Public License
  34. along with this program. If not, see <http://www.gnu.org/licenses/>.
  35.  
  36. **************************************************************************/
  37.  
  38. XPath = Xpath = xpath = function()
  39. {
  40. var a = Array.prototype.slice.call(arguments), // args
  41. e = a[0], // expression
  42. c = a[1], // context
  43. t = a[2]; // type
  44.  
  45. if (typeof c == "function")
  46. {
  47. t = c;
  48. c = null;
  49. }
  50. if (!c)
  51. c = document.documentElement||document;
  52. var d = c.ownerDocument || c;
  53. e = d.createExpression(e, function(p)
  54. {
  55. var o = d.createNSResolver(c).lookupNamespaceURI(p);
  56.  
  57. if (o)
  58. return o;
  59. else switch (c.contentType)
  60. {
  61. case "text/xhtml":
  62. case "application/xhtml+xml":
  63. return "http://www.w3.org/1999/xhtml";
  64. default:
  65. return "";
  66. }
  67. });
  68.  
  69. switch (t)
  70. {
  71. case String:
  72. return e.evaluate(c, XPathResult.STRING_TYPE, null).stringValue;
  73. case Number:
  74. return e.evaluate(c, XPathResult.NUMBER_TYPE, null).numberValue;
  75. case Boolean:
  76. return e.evaluate(c, XPathResult.BOOLEAN_TYPE, null).booleanValue;
  77. case Array:
  78. var r = e.evaluate(c, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null),
  79. o = [];
  80.  
  81. for ( var ai = 0 , at = r.snapshotLength ; ai < at ; ++ai )
  82. o.push(r.snapshotItem(ai));
  83.  
  84. return o;
  85. case undefined:
  86. r = e.evaluate(c, XPathResult.ANY_TYPE, null);
  87. switch (r.resultType)
  88. {
  89. case XPathResult.STRING_TYPE:
  90. return r.stringValue;
  91. case XPathResult.NUMBER_TYPE:
  92. return r.numberValue;
  93. case XPathResult.BOOLEAN_TYPE:
  94. return r.booleanValue;
  95. case XPathResult.UNORDERED_NODE_ITERATOR_TYPE:
  96. var o = [], i;
  97. while (i == r.iterateNext())
  98. o.push(i);
  99.  
  100. return o;
  101. }
  102. return null;
  103. default:
  104. throw(TypeError("xpath: specified type is not valid type."));
  105. }
  106. };