Guest User

Untitled

a guest
Aug 18th, 2025
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Instagram iframe reloader
  3. // @namespace    https://example.com/
  4. // @version      1.1
  5. // @description  Adds reload buttons to Instagram iframes without auto-reload
  6. // @include      */threads/*
  7. // @grant        none
  8. // ==/UserScript==
  9.  
  10. (function(){
  11.   'use strict';
  12.  
  13.   const DEBUG = false; // true = logs, false = silent
  14.   const BUTTON_POSITION = 'right'; // 'right' | 'left' | 'top' | 'bottom'
  15.  
  16.   const STYLE_ID = 'insta-iframe-reloader-style';
  17.   const BTN_CLASS = 'insta-reload-btn';
  18.   const WRAP_CLASS = 'insta-iframe-wrapper';
  19.   const FLAG = 'reloadAttached';
  20.   const SELECTOR = 'iframe[data-s9e-mediaembed="instagram"]';
  21.  
  22.   function log(...args){ if(DEBUG) console.log('[InstaReloader]', ...args); }
  23.  
  24.   if (!document.getElementById(STYLE_ID)) {
  25.     const style = document.createElement('style');
  26.     style.id = STYLE_ID;
  27.     style.textContent = `
  28.       .${WRAP_CLASS} {
  29.         position: relative;
  30.         display: inline-block;
  31.         padding: ${BUTTON_POSITION==='right'||BUTTON_POSITION==='left' ? '0 32px' : '32px 0'};
  32.       }
  33.       .${BTN_CLASS} {
  34.         position: absolute;
  35.         background: #fff;
  36.         border: 1px solid #ccc;
  37.         border-radius: 50%;
  38.         width: 24px;
  39.         height: 24px;
  40.         font-size: 14px;
  41.         line-height: 22px;
  42.         text-align: center;
  43.         cursor: pointer;
  44.         box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  45.         z-index: 2147483647;
  46.         user-select: none;
  47.       }
  48.       .${BTN_CLASS}:hover { filter: brightness(0.97); }
  49.     `;
  50.     document.head.appendChild(style);
  51.   }
  52.  
  53.   function positionButton(btn){
  54.     if(BUTTON_POSITION==='right'){
  55.       btn.style.right='-28px'; btn.style.top='50%'; btn.style.transform='translateY(-50%)';
  56.     } else if(BUTTON_POSITION==='left'){
  57.       btn.style.left='-28px'; btn.style.top='50%'; btn.style.transform='translateY(-50%)';
  58.     } else if(BUTTON_POSITION==='top'){
  59.       btn.style.top='-28px'; btn.style.left='50%'; btn.style.transform='translateX(-50%)';
  60.     } else if(BUTTON_POSITION==='bottom'){
  61.       btn.style.bottom='-28px'; btn.style.left='50%'; btn.style.transform='translateX(-50%)';
  62.     }
  63.   }
  64.  
  65.   function forceReload(iframe){
  66.     const prev=iframe.getAttribute('src')||'';
  67.     log('Reload requested:', prev);
  68.     try {
  69.       const url=new URL(prev, document.baseURI);
  70.       url.searchParams.set('_reload', Date.now().toString());
  71.       const next=url.toString();
  72.       if(next===prev){
  73.         iframe.src='about:blank';
  74.         requestAnimationFrame(()=>iframe.src=prev);
  75.       } else {
  76.         iframe.src=next;
  77.       }
  78.     } catch {
  79.       iframe.src='about:blank';
  80.       requestAnimationFrame(()=>iframe.src=prev);
  81.     }
  82.   }
  83.  
  84.   function addButton(iframe){
  85.     if(iframe.dataset[FLAG]==='true') return;
  86.     iframe.dataset[FLAG]='true';
  87.  
  88.     let wrapper=iframe.parentElement;
  89.     if(!wrapper.classList.contains(WRAP_CLASS)){
  90.       wrapper=document.createElement('span');
  91.       wrapper.className=WRAP_CLASS;
  92.       iframe.parentNode.insertBefore(wrapper, iframe);
  93.       wrapper.appendChild(iframe);
  94.     }
  95.  
  96.     const btn=document.createElement('div');
  97.     btn.textContent='⟳';
  98.     btn.className=BTN_CLASS;
  99.     btn.title='Reload this iframe';
  100.     positionButton(btn);
  101.     btn.addEventListener('click',e=>{
  102.       e.preventDefault(); e.stopPropagation(); forceReload(iframe);
  103.     });
  104.     wrapper.appendChild(btn);
  105.   }
  106.  
  107.   let scheduled=false;
  108.   function scan(){
  109.     if(scheduled) return;
  110.     scheduled=true;
  111.     setTimeout(()=>{
  112.       document.querySelectorAll(SELECTOR).forEach(addButton);
  113.       scheduled=false;
  114.     },300);
  115.   }
  116.  
  117.   scan();
  118.   const obs=new MutationObserver(scan);
  119.   obs.observe(document.body,{childList:true,subtree:true});
  120. })();
  121.  
Advertisement
Add Comment
Please, Sign In to add comment