Fanfiction.net: Not-Crossover Link

Add link to not-Crossover page to Fanfiction.net.

目前为 2018-06-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fanfiction.net: Not-Crossover Link
  3. // @namespace https://greasyfork.org/en/users/163551-vannius
  4. // @version 1.4
  5. // @description Add link to not-Crossover page to Fanfiction.net.
  6. // @author Vannius
  7. // @match https://www.fanfiction.net/crossovers/*
  8. // @match https://www.fanfiction.net/*Crossovers/*
  9. // @grant GM_openInTab
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. // Config
  14. const OPEN_IN_NEW_TAB = true;
  15.  
  16. // Functions
  17. // Replace and delete prohibited characters.
  18. function deleteProhibitedCharacter(text) {
  19. return text.replace(/['|\.|\/| & | - | + ]+/g, ' ').replace(/[,\+!:;\|☆★!?]+/g, '');
  20. }
  21.  
  22. // Return imgTag with click event
  23. function addClickToNotCrossover(imgTag, url) {
  24. imgTag.addEventListener('click', function(e) {
  25. e.preventDefault();
  26. fetch(url, { method: 'GET',
  27. mode: 'same-origin',
  28. cache: 'default' })
  29. .then(response => response.text())
  30. .then(body => {
  31. const doc = document.implementation.createHTMLDocument('myBody');
  32. doc.documentElement.innerHTML = body;
  33. const content = doc.getElementById('content_wrapper_inner');
  34.  
  35. const list = content.getElementsByClassName('z-list');
  36.  
  37. let destination = '';
  38. if (list.length) destination = url; // Bingo.
  39.  
  40. const candidates = content.querySelectorAll('.gui_normal a');
  41. if (candidates.length == 1) {
  42. destination = candidates[0].href; // Only candidate.
  43. } else if (candidates.length > 1) {
  44. destination = url; // Suggestion page. There can be sevaral candidates for same fandom.
  45. }
  46.  
  47. // Move to Not-Crossover page.
  48. if (OPEN_IN_NEW_TAB) {
  49. GM_openInTab(destination);
  50. } else {
  51. window.location.href = destination;
  52. }
  53. }).catch(error=> console.log(error));
  54. });
  55. return imgTag;
  56. }
  57.  
  58. // Main
  59. const splitPath = window.location.href.split('/');
  60.  
  61. if (/^.+[-\_]and[-\_].+[-\_]Crossovers$/.test(splitPath[3])) {
  62. // Scrape each fandom link
  63. const divTag = document.getElementById('content_wrapper_inner');
  64. const titleTags = [divTag.children[1], divTag.children[2]];
  65.  
  66. for (let titleTag of titleTags) {
  67. // Make joinTitle by replacing and deleting prohibited symbols
  68. const joinTitle = deleteProhibitedCharacter(titleTag.textContent).split(' ').join('-');
  69. const url = [window.location.origin, 'anime', joinTitle, ''].join('/');
  70.  
  71. // Make link to Not-Crossover
  72. const imgTag = document.getElementById('content_wrapper_inner').children[0];
  73. const addImgTag = addClickToNotCrossover(imgTag.cloneNode(false), url);
  74. addImgTag.title = "Not-Crossover";
  75. addImgTag.style.transform = "scale(-1, 1)";
  76.  
  77. // Add link and place adjustment
  78. const fragment = document.createDocumentFragment();
  79. fragment.appendChild(addImgTag);
  80. fragment.appendChild(document.createTextNode(' '));
  81. imgTag.parentNode.insertBefore(fragment, titleTag);
  82. }
  83. } else if (splitPath[3] == 'crossovers' || /^.+[-\_]Crossovers$/.test(splitPath[3])) {
  84. // Make title and url by splitPath[4] or splitPath[3]
  85. const title = (splitPath[3] == 'crossovers') ? splitPath[4] : splitPath[3].slice(0, - "-Crossovers".length);
  86. const url = [window.location.origin, 'anime', title, ''].join('/');
  87.  
  88. // Make link to Not-Crossover
  89. const divTag = document.getElementById('content_wrapper_inner');
  90. const imgTag = (splitPath[3] == 'crossovers') ? divTag.children[2]: divTag.children[0];
  91. const addImgTag = addClickToNotCrossover(imgTag.cloneNode(false), url);
  92. addImgTag.title = "Not-Crossover";
  93. addImgTag.style.transform = "scale(-1, 1)";
  94.  
  95. // Add link and place adjustment
  96. const fragment = document.createDocumentFragment();
  97. fragment.appendChild(document.createTextNode(' '));
  98. fragment.appendChild(addImgTag);
  99. imgTag.parentNode.insertBefore(fragment, imgTag.nextSibling);
  100. }
  101. })();