Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Twitter/X User Media Tab Deduplicator
- // @namespace https://x.com
- // @version 1.0
- // @description Hide all duplicate media in user profiles media tab
- // @author KBD99
- // @match https://x.com/*/media*
- // @grant none
- // ==/UserScript==
- (function () {
- 'use strict';
- let dedupEnabled = true;
- const HASH_KEY = 'twitterMediaHashes';
- let seenHashes = JSON.parse(localStorage.getItem(HASH_KEY) || '{}');
- // UI Toggle Button
- const toggleButton = document.createElement('button');
- toggleButton.textContent = 'Dedup ON';
- Object.assign(toggleButton.style, {
- position: 'fixed',
- top: '10px',
- right: '10px',
- zIndex: 9999,
- padding: '6px',
- fontSize: '10px',
- backgroundColor: '#1da1f2',
- color: 'white',
- border: 'none',
- borderRadius: '3px',
- cursor: 'pointer',
- });
- toggleButton.onclick = () => {
- dedupEnabled = !dedupEnabled;
- toggleButton.textContent = dedupEnabled ? 'Dedup ON' : 'Dedup OFF';
- if (dedupEnabled) runDeduplication();
- };
- // Clear Cache Button
- const clearButton = document.createElement('button');
- clearButton.textContent = 'Clear Cache';
- Object.assign(clearButton.style, {
- position: 'fixed',
- top: '10px',
- right: '110px',
- zIndex: 9999,
- padding: '6px',
- fontSize: '10px',
- backgroundColor: '#e0245e',
- color: 'white',
- border: 'none',
- borderRadius: '3px',
- cursor: 'pointer',
- });
- clearButton.onclick = () => {
- localStorage.removeItem(HASH_KEY);
- seenHashes = {};
- alert('Media cache cleared! Reload to see all items again.');
- };
- document.body.appendChild(toggleButton);
- document.body.appendChild(clearButton);
- // Generate visual hash from image or video
- function getVisualHash(el) {
- try {
- const canvas = document.createElement('canvas');
- canvas.width = canvas.height = 32;
- const ctx = canvas.getContext('2d');
- ctx.drawImage(el, 0, 0, 32, 32);
- return canvas.toDataURL().slice(0, 100);
- } catch {
- return null;
- }
- }
- // Deduplication Logic
- function runDeduplication() {
- if (!dedupEnabled) return;
- const mediaElements = document.querySelectorAll('img[src], video[src]');
- mediaElements.forEach(el => {
- if (!el.complete || el.naturalWidth === 0) return;
- const hash = getVisualHash(el);
- if (!hash) return;
- const container = el.closest('div[role="button"]') || el.closest('article');
- if (!container) return;
- if (seenHashes[hash]) {
- container.style.setProperty('display', 'none', 'important');
- } else {
- seenHashes[hash] = true;
- localStorage.setItem(HASH_KEY, JSON.stringify(seenHashes));
- }
- });
- }
- // Observe for lazy-loaded media
- const observer = new MutationObserver(() => runDeduplication());
- observer.observe(document.body, { childList: true, subtree: true });
- window.addEventListener('load', () => setTimeout(runDeduplication, 3000));
- })();
- `
Advertisement
Add Comment
Please, Sign In to add comment