Advertisement
Guest User

e621 Temp Sticky Blacklist Prototype Extension

a guest
Nov 18th, 2022
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.67 KB | Source Code | 0 0
  1. // Create folder e621-extension, then put the following files inside it.
  2. // Go to chrome://extensions/ put yourself in developer mode (top right), then "Load Unpacked Extension"; the _folder_ you created is the extension.
  3.  
  4. // manifest.json
  5. {
  6.     "manifest_version": 3,
  7.     "name": "e621 Blacklist Toggle",
  8.     "version": "0.1",
  9.     "content_scripts": [{
  10.         "matches": ["https://e621.net/*", "https://e926.net/*"],
  11.         "js": ["content-script.js"]
  12.     }],
  13.     "web_accessible_resources": [{
  14.         "resources": ["e621.user.js"],
  15.         "matches": ["https://e621.net/*", "https://e926.net/*"],
  16.         "use_dynamic_url": true
  17.     }]
  18. }
  19.  
  20. // content-script.js
  21. // Credit: https://stackoverflow.com/a/9517879
  22. var s = document.createElement('script');
  23. s.src = chrome.runtime.getURL('e621.user.js');
  24. s.onload = function() {
  25.         this.remove();
  26. };
  27. (document.head || document.documentElement).appendChild(s);
  28.  
  29. // e621.user.js
  30. // ==UserScript==
  31. // @name Blacklist Toggle
  32. // @match https://e621.net/*
  33. // @match https://e926.net/*
  34. // ==/UserScript==
  35.  
  36. (function() {
  37.     function runWhenReady(run) {
  38.         if (document.readyState != 'loading')
  39.             run();
  40.         else
  41.             document.addEventListener('DOMContentLoaded', run);
  42.     }
  43.     runWhenReady(function() {
  44.         function toggle(set, key) {
  45.             set.delete(key) || set.add(key);
  46.         }
  47.         let off;
  48.         function setOff(keys) {
  49.             off = new Set(keys);
  50.             off.forEach(key => {
  51.                 Danbooru.Blacklist.lineSet(key, false);
  52.             })
  53.             if (off.size)
  54.                 Danbooru.Blacklist.apply();
  55.         }
  56.  
  57.         // Expire after six hours
  58.         let expire = new Date(
  59.             new Date(
  60.                 window.localStorage.getItem('blacklist_last')
  61.             ).getTime()
  62.             + 6 * 60 * 60 * 1000
  63.         );
  64.         if (expire < new Date())
  65.             window.localStorage.removeItem('blacklist_off');
  66.  
  67.         setOff(JSON.parse(window.localStorage.getItem('blacklist_off')) || []);
  68.         addEventListener('storage', e => {
  69.             // wipe on Disable/Re-enable all blacklist entries
  70.             if (e.key === 'dab') {
  71.                 window.localStorage.removeItem('blacklist_off');
  72.                 Danbooru.Blacklist.entries.forEach(entry => {
  73.                     entry.disabled = !!JSON.parse(e.newValue);
  74.                 })
  75.                 Danbooru.Blacklist.apply();
  76.                 return;
  77.             }
  78.  
  79.             if (e.key !== 'blacklist_off')
  80.                 return;
  81.  
  82.             // re-enable all, then disable from local storage
  83.             Danbooru.Blacklist.entries.forEach(entry => {
  84.                 entry.disabled = false;
  85.             })
  86.             setOff(JSON.parse(e.newValue) || []);
  87.         });
  88.  
  89.         let lineToggle = Danbooru.Blacklist.lineToggle; // monkey patch it
  90.         Danbooru.Blacklist.lineToggle = (t) => {
  91.             lineToggle(t);
  92.             window.localStorage.setItem('blacklist_last', new Date().toJSON());
  93.             toggle(off, t);
  94.             window.localStorage.setItem('blacklist_off', JSON.stringify(Array.from(off.keys())));
  95.         };
  96.     });
  97. })();
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement