e-hentai 搜索輔助 快速添加標籤

點擊按鈕快速添加指定標籤至搜索框

  1. // ==UserScript==
  2. // @name e-hentai 搜索輔助 快速添加標籤
  3. // @namespace https://greasyfork.org/scripts/500650
  4. // @version 0.3
  5. // @description 點擊按鈕快速添加指定標籤至搜索框
  6. // @author fmnijk
  7. // @match https://e-hentai.org/*
  8. // @match https://exhentai.org/*
  9. // @icon https://www.google.com/s2/favicons?domain=e-hentai.org
  10. // @grant none
  11. // @run-at document-end
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. var tags = {
  19. 'language:chinese$': '中文',
  20. 'other:"full color$"': '全彩',
  21. 'other:uncensored$': '無碼'
  22. };
  23. var nextline = [-1];
  24.  
  25. function createButtons(targetElement) {
  26. var style = document.createElement('style');
  27. style.textContent = `
  28. .custom-button {
  29. color: #f1f1f1;
  30. border-color: #1357df;
  31. background: radial-gradient(#1357df, #3377FF) !important;
  32. padding: 2px 5px;
  33. border-radius: 3px;
  34. text-decoration: none;
  35. }
  36. `;
  37. document.head.appendChild(style);
  38.  
  39. var fragment = document.createDocumentFragment();
  40. Object.entries(tags).forEach(function([tag, buttonText], index) {
  41. var span = document.createElement('span');
  42. span.innerHTML = '<a href="javascript:void(0);" class="custom-button">' + buttonText + '</a>';
  43. if (nextline.includes(index + 1) || (nextline.includes(-1) && index === Object.keys(tags).length - 1)) {
  44. span.innerHTML += '<br>';
  45. } else {
  46. span.innerHTML += ' &nbsp;';
  47. }
  48. span.querySelector('a').onclick = function(e) {
  49. e.preventDefault();
  50. if (window.getSelection) {
  51. window.getSelection().removeAllRanges();
  52. } else if (document.selection) {
  53. document.selection.empty();
  54. }
  55. var searchInput = document.querySelector('#f_search');
  56. if (searchInput) {
  57. var currentValue = searchInput.value;
  58. var tagToCheck = tag.trim();
  59. if (currentValue.includes(tagToCheck)) {
  60. var regex = new RegExp("\\s?" + tagToCheck.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') + "\\s?");
  61. searchInput.value = currentValue.replace(regex, ' ').trim();
  62. } else {
  63. searchInput.value = (currentValue ? currentValue + ' ' : '') + tagToCheck;
  64. }
  65. }
  66. };
  67. fragment.appendChild(span);
  68. });
  69. targetElement.insertBefore(fragment, targetElement.firstChild);
  70. }
  71.  
  72. function checkForTargetElement() {
  73. var targetElement = document.querySelector('#searchbox > form > div:nth-child(4)');
  74. if (targetElement) {
  75. createButtons(targetElement);
  76. observer.disconnect();
  77. }
  78. }
  79.  
  80. var observer = new MutationObserver(function(mutations) {
  81. checkForTargetElement();
  82. });
  83.  
  84. observer.observe(document.body, {
  85. childList: true,
  86. subtree: true
  87. });
  88.  
  89. // 初始檢查
  90. checkForTargetElement();
  91. })();