MrSableye

Expand Dogars Script

Feb 21st, 2021 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Expand DONGars
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.0.0
  5. // @description  Expands Dogars links into their full sets.
  6. // @author       Mr. Sableye
  7. // @include      https://boards.4channel.org/*
  8. // @include      http://boards.4channel.org/*
  9. // @grant        GM.xmlHttpRequest
  10. // @run-at       document-body
  11. // ==/UserScript==
  12.  
  13. let updatedPosts = {};
  14.  
  15. // https://github.com/shoedrip-unbound/dogars-frontend/blob/master/src/utils.ts#L70
  16. const setToString = (set) => {
  17.     let res = '';
  18.     if (set.name)
  19.         res += `${set.name} (${set.species})`;
  20.     else
  21.         res += set.species;
  22.     if (set.gender && set.gender != '' && set.gender != 'N')
  23.         res += ` (${set.gender})`;
  24.     if (set.item && set.item != '')
  25.         res += ` @ ${set.item}`;
  26.     res += `\nAbility: ${set.ability}\n`;
  27.     if (set.level && set.level != 100)
  28.         res += `Level: ${set.level}\n`;
  29.     if (set.shiny)
  30.         res += 'Shiny: Yes\n';
  31.  
  32.     if (set.happiness !== undefined && set.happiness !== null && set.happiness < 255)
  33.         res += `Happiness: ${set.happiness}\n`;
  34.     let buildstr = (t, f) => ['HP', 'Atk', 'Def', 'SpA', 'SpD', 'Spe']
  35.         .filter(s => set[`${s.toLowerCase()}_${t}`] != f)
  36.         .map(s => set[`${s.toLowerCase()}_${t}`] + ' ' + s)
  37.         .join(' / ');
  38.  
  39.     let evstr = buildstr('ev', 0);
  40.     let ivstr = buildstr('iv', 31);
  41.     if (evstr != '')
  42.         res += `EVs: ${evstr}\n`;
  43.     let neutral = ['Serious', 'Bashful', 'Docile', 'Hardy', 'Quirky'];
  44.     let nature = neutral.some(n => n == set.nature);
  45.     if (set.nature && !nature)
  46.         res += `${set.nature} Nature\n`;
  47.     if (ivstr != '')
  48.         res += `IVs: ${ivstr}\n`;
  49.     res += [1, 2, 3, 4]
  50.         .map(d => 'move_' + d)
  51.         .filter(n => set[n])
  52.         .map(n => `- ${set[n]}`)
  53.         .join('\n') + '\n';
  54.  
  55.     if (set.creator || set.hash) {
  56.       res += `Creator: ${set.creator || 'Anonymous'}${(set.hash && `#${set.hash}`) || ''}\n`;
  57.     }
  58.  
  59.     if (set.description) {
  60.       res += `Description: ${set.description}\n`;
  61.     }
  62.  
  63.     return res;
  64. };
  65.  
  66. const getSet = (setId) => fetch(`https://dogars.ga/api/sets/${setId}`)
  67.     .then(response => response.text())
  68.     .then(responseText => JSON.parse(responseText))
  69.     .then(responseJson => setToString(responseJson));
  70.  
  71. const dogarsLinkRegex = /(https?:\/\/)?dogars.ga\/set\/(?<setId>[0-9]+)/gi;
  72.  
  73. const expandDogars = async () => {
  74.   const postElements = document.getElementsByClassName('postMessage');
  75.  
  76.   for (const postElement of postElements) {
  77.     if (!updatedPosts[postElement.id]) {
  78.       const postElementText = postElement.textContent;
  79.       let match = dogarsLinkRegex.exec(postElementText);
  80.    
  81.       while (match !== null) {
  82.         if (match?.groups?.['setId']) {
  83.           const set = await getSet(match?.groups?.['setId']);
  84.    
  85.           const setNode = document.createElement('pre');
  86.           setNode.classList.add('prettyprint');
  87.           setNode.classList.add('prettyprinted');
  88.    
  89.           const setTextNode = document.createTextNode(set);
  90.           setNode.appendChild(setTextNode);
  91.    
  92.           const lineBreak = document.createElement('br');
  93.           postElement.appendChild(lineBreak);
  94.           postElement.appendChild(setNode);
  95.         }
  96.    
  97.         match = dogarsLinkRegex.exec(postElementText);
  98.       }
  99.  
  100.       updatedPosts[postElement.id] = true;
  101.     }
  102.   }
  103. };
  104.  
  105. setTimeout(expandDogars, 2000);
  106. setInterval(expandDogars, 30000);
  107.  
Add Comment
Please, Sign In to add comment