Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Instagram iframe reloader
- // @namespace https://example.com/
- // @version 1.1
- // @description Adds reload buttons to Instagram iframes without auto-reload
- // @include */threads/*
- // @grant none
- // ==/UserScript==
- (function(){
- 'use strict';
- const DEBUG = false; // true = logs, false = silent
- const BUTTON_POSITION = 'right'; // 'right' | 'left' | 'top' | 'bottom'
- const STYLE_ID = 'insta-iframe-reloader-style';
- const BTN_CLASS = 'insta-reload-btn';
- const WRAP_CLASS = 'insta-iframe-wrapper';
- const FLAG = 'reloadAttached';
- const SELECTOR = 'iframe[data-s9e-mediaembed="instagram"]';
- function log(...args){ if(DEBUG) console.log('[InstaReloader]', ...args); }
- if (!document.getElementById(STYLE_ID)) {
- const style = document.createElement('style');
- style.id = STYLE_ID;
- style.textContent = `
- .${WRAP_CLASS} {
- position: relative;
- display: inline-block;
- padding: ${BUTTON_POSITION==='right'||BUTTON_POSITION==='left' ? '0 32px' : '32px 0'};
- }
- .${BTN_CLASS} {
- position: absolute;
- background: #fff;
- border: 1px solid #ccc;
- border-radius: 50%;
- width: 24px;
- height: 24px;
- font-size: 14px;
- line-height: 22px;
- text-align: center;
- cursor: pointer;
- box-shadow: 0 2px 6px rgba(0,0,0,0.2);
- z-index: 2147483647;
- user-select: none;
- }
- .${BTN_CLASS}:hover { filter: brightness(0.97); }
- `;
- document.head.appendChild(style);
- }
- function positionButton(btn){
- if(BUTTON_POSITION==='right'){
- btn.style.right='-28px'; btn.style.top='50%'; btn.style.transform='translateY(-50%)';
- } else if(BUTTON_POSITION==='left'){
- btn.style.left='-28px'; btn.style.top='50%'; btn.style.transform='translateY(-50%)';
- } else if(BUTTON_POSITION==='top'){
- btn.style.top='-28px'; btn.style.left='50%'; btn.style.transform='translateX(-50%)';
- } else if(BUTTON_POSITION==='bottom'){
- btn.style.bottom='-28px'; btn.style.left='50%'; btn.style.transform='translateX(-50%)';
- }
- }
- function forceReload(iframe){
- const prev=iframe.getAttribute('src')||'';
- log('Reload requested:', prev);
- try {
- const url=new URL(prev, document.baseURI);
- url.searchParams.set('_reload', Date.now().toString());
- const next=url.toString();
- if(next===prev){
- iframe.src='about:blank';
- requestAnimationFrame(()=>iframe.src=prev);
- } else {
- iframe.src=next;
- }
- } catch {
- iframe.src='about:blank';
- requestAnimationFrame(()=>iframe.src=prev);
- }
- }
- function addButton(iframe){
- if(iframe.dataset[FLAG]==='true') return;
- iframe.dataset[FLAG]='true';
- let wrapper=iframe.parentElement;
- if(!wrapper.classList.contains(WRAP_CLASS)){
- wrapper=document.createElement('span');
- wrapper.className=WRAP_CLASS;
- iframe.parentNode.insertBefore(wrapper, iframe);
- wrapper.appendChild(iframe);
- }
- const btn=document.createElement('div');
- btn.textContent='⟳';
- btn.className=BTN_CLASS;
- btn.title='Reload this iframe';
- positionButton(btn);
- btn.addEventListener('click',e=>{
- e.preventDefault(); e.stopPropagation(); forceReload(iframe);
- });
- wrapper.appendChild(btn);
- }
- let scheduled=false;
- function scan(){
- if(scheduled) return;
- scheduled=true;
- setTimeout(()=>{
- document.querySelectorAll(SELECTOR).forEach(addButton);
- scheduled=false;
- },300);
- }
- scan();
- const obs=new MutationObserver(scan);
- obs.observe(document.body,{childList:true,subtree:true});
- })();
Advertisement
Add Comment
Please, Sign In to add comment