custom DLSite linker

Changes RJ/Exxxxxx numbers to links to DLSite

  1. // ==UserScript==
  2. // @name custom DLSite linker
  3. // @namespace https://greasyfork.org/en/users/4367-d-p
  4. // @version 1.1.1
  5. // @description Changes RJ/Exxxxxx numbers to links to DLSite
  6. // @match http://www.ulmf.org/*
  7. // @match http://www.hongfire.com/*
  8. // @include *
  9. // ==/UserScript==
  10.  
  11.  
  12. /*
  13. based on: http://userscripts-mirror.org/scripts/review/155521
  14.  
  15. 1.1.1
  16. commented out document.normalize(); it was causing http://overwatchlf.com/ to break
  17.  
  18. 1.1
  19. changed so it works on all websites. forced all rj numbers to be uppercase and specified a length of 6 numbers.
  20. */
  21.  
  22. // MAXIMUM LENGTH OF LEFT STRING
  23. // "«".length = 11
  24. var MAX_LEFT_STR = 11;
  25. var fixBalanced = function(text, leftStr)
  26. {
  27. var index = -1;
  28. switch (leftStr.charAt(leftStr.length - 1))
  29. {
  30. case "`": index = text.indexOf("'"); break; // ` '
  31. case "'": index = text.indexOf("'"); break; // ' '
  32. case "(": index = text.indexOf(")"); break; // ( )
  33. case "[": index = text.indexOf("]"); break; // [ ]
  34. }
  35. if (index > -1)
  36. {
  37. return text.substring(0, index);
  38. }
  39. leftStr = leftStr.substring(leftStr.length - MAX_LEFT_STR);
  40. if (/&lt;$/.test(leftStr)) { index = text.indexOf("&gt;"); } // < >
  41. else { if (/&amp;lt;$/.test(leftStr)) { index = text.indexOf("&amp;gt;"); } // < >
  42. else { if (/&amp;#60;$/.test(leftStr)) { index = text.indexOf("&amp;#62;"); } // < >
  43. else { if (/&amp;quot;$/.test(leftStr)) { index = text.indexOf("&amp;quot;"); } // " "
  44. else { if (/&amp;#34;$/.test(leftStr)) { index = text.indexOf("&amp;#34;"); } // " "
  45. else { if (/&amp;#96;$/.test(leftStr)) { index = text.indexOf("'"); } // ` '
  46. else { if (/&amp;laquo;$/.test(leftStr)) { index = text.indexOf("&amp;raquo;"); } // ≪ ≫
  47. else { if (/&amp;#171;$/.test(leftStr)) { index = text.indexOf("&amp;#187;"); } // ≪ ≫
  48. }}}}}}}
  49. if (index > -1)
  50. {
  51. return text.substring(0, index);
  52. }
  53. return text;
  54. };
  55. var textToLink = function(nodeValue)
  56. {
  57. var changesMade = false;
  58. nodeValue = nodeValue.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
  59. var DLSiteRegEx = new RegExp('r(j|e)[0-9]{6}','gi');
  60. var matches = null;
  61. var text = null;
  62. var index = null;
  63. var leftStr = null;
  64. var link = null;
  65. var anchor = null;
  66. var fromIndex = 0;
  67. while ((matches = nodeValue.substring(fromIndex).match(DLSiteRegEx)) !== null)
  68. {
  69. text = matches[0];
  70. index = nodeValue.indexOf(text, fromIndex);
  71. leftStr = nodeValue.substring(0, index);
  72. text = fixBalanced(text, leftStr);
  73. fromIndex = index + text.length;
  74. if (/^([aaaoou]|\.\w)/i.test(nodeValue.substring(fromIndex, fromIndex + 2)))
  75. {
  76. continue;
  77. }
  78. link = nodeValue.substring(index, index + text.length)
  79. if (link.match(/rj/i)) {
  80. anchor = "<a href=\"http://www.dlsite.com/maniax/work/=/product_id/" + link.toUpperCase() + ".html\">" + text + "</a>";
  81. } else {
  82. anchor = "<a href=\"http://www.dlsite.com/ecchi-eng/work/=/product_id/" + link.toUpperCase() + ".html\">" + text + "</a>";
  83. }
  84. nodeValue = leftStr + anchor + nodeValue.substring(fromIndex);
  85. fromIndex = index + anchor.length;
  86. changesMade = true;
  87. }
  88. if (!changesMade)
  89. {
  90. return null;
  91. }
  92. else
  93. {
  94. return nodeValue;
  95. }
  96. };
  97. var main = function()
  98. {
  99. //document.normalize(); // this line causes http://overwatchlf.com/ to break. commented out
  100. var elements = null;
  101. var element = null;
  102. var nodeValue = null;
  103. elements = document.evaluate(".//text()[not(ancestor::a) and not(ancestor::button) and not(ancestor::label) and not(ancestor::legend) and not(ancestor::option) and not(ancestor::script) and not(ancestor::select) and not(ancestor::style) and not(ancestor::textarea) and not(ancestor::title)]", document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  104. if (!elements || elements.snapshotLength === 0)
  105. {
  106. return;
  107. }
  108. var span = null;
  109. for (var i = 0; i < elements.snapshotLength; i++)
  110. {
  111. element = elements.snapshotItem(i);
  112. nodeValue = textToLink(element.nodeValue);
  113. if (nodeValue)
  114. {
  115. span = document.createElement("span");
  116. span.innerHTML = nodeValue;
  117. element.parentNode.replaceChild(span, element);
  118. }
  119. }
  120. };
  121. main();