Nyaa link

Provides nyaa link

  1. // ==UserScript==
  2. // @name Nyaa link
  3. // @license MIT
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.1
  6. // @description Provides nyaa link
  7. // @author You
  8. // @match *://*.kitsu.app/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Function to handle the main logic
  16. const processPage = () => {
  17. try {
  18. // Find the <li> element containing <strong>Japanese (Romaji)</strong>
  19. const targetLi = Array.from(document.querySelectorAll('li')).find(li => {
  20. return li.querySelector('strong')?.textContent.trim() === 'Japanese (Romaji)';
  21. });
  22.  
  23. if (!targetLi) return; // Exit if not found
  24.  
  25. // Extract the text inside the <span> element within the found <li>
  26. const spanContent = targetLi.querySelector('span')?.textContent.trim();
  27.  
  28. if (!spanContent) return; // Exit if no span content
  29.  
  30. // Sanitize the string for use in a URL
  31. const sanitizedString = encodeURIComponent(spanContent);
  32.  
  33. // Construct the URL
  34. const queryUrl = `https://nyaa.si/?q=${sanitizedString}`;
  35.  
  36. // Check if the link already exists to avoid duplicates
  37. if (document.querySelector(`a[href='${queryUrl}']`)) return;
  38.  
  39. // Create an image element for the favicon
  40. const faviconImg = document.createElement('img');
  41. faviconImg.src = 'https://nyaa.si/static/favicon.png';
  42. faviconImg.alt = 'Link';
  43. faviconImg.style.width = '16px'; // Optional: Adjust the size
  44. faviconImg.style.height = '16px';
  45.  
  46. // Create the link element
  47. const linkElement = document.createElement('a');
  48. linkElement.href = queryUrl;
  49. linkElement.target = '_blank'; // Open in a new tab
  50. linkElement.appendChild(faviconImg);
  51. linkElement.className = 'nyaa-link'; // Add a unique class name
  52.  
  53. // Append the link to the end of the element with class "media--title"
  54. const targetContainer = document.querySelector('.media--title');
  55.  
  56. if (targetContainer) {
  57. const existingLinks = document.querySelectorAll('.nyaa-link');
  58. existingLinks.forEach(link => link.remove());
  59. targetContainer.appendChild(linkElement);
  60. }
  61. } catch (error) {
  62. console.error('Error occurred in the script:', error);
  63. }
  64. };
  65.  
  66. // Observe DOM changes continuously for dynamic React updates
  67. const observeDOMChanges = () => {
  68. const observer = new MutationObserver(() => {
  69. processPage(); // Check and process whenever the DOM changes
  70. });
  71.  
  72. observer.observe(document.body, { childList: true, subtree: true });
  73. };
  74.  
  75. // Observe URL changes (useful for React sites)
  76. let previousUrl = location.href;
  77. const observeURLChanges = () => {
  78. const urlObserver = new MutationObserver(() => {
  79. if (location.href !== previousUrl) {
  80. previousUrl = location.href;
  81. processPage(); // Re-run the logic on URL change
  82. }
  83. });
  84.  
  85. urlObserver.observe(document.body, { childList: true, subtree: true });
  86. };
  87.  
  88. // Initial setup
  89. processPage();
  90. observeDOMChanges();
  91. observeURLChanges();
  92. })();