Includes : XPath

xpath Function

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