// ==UserScript== // @name 全站链接小窗 // @namespace https://qq5855144.github.io/ // @version 1.0.5 // @description 全站链接小窗浏览器:多标签、历史导航、拖拽高度、站点记忆、媒体预览、黑白名单、沉浸式顶栏、手势滑动切换标签(与页面横向滚动原生互斥,不误触);小窗内跳转地址同步(含跨域回传) // @match *://*/* // @run-at document-idle // @grant none // ==/UserScript== (function () { 'use strict'; var FRAME_MODE = window.top !== window.self; if (!FRAME_MODE) { if (window.__MT_GLOBAL_LINK_WINDOW__) return; window.__MT_GLOBAL_LINK_WINDOW__ = true; try { Object.defineProperty(window, '__MT_SW_PANEL_HOST__', { get: function () { return { openMode: cfg.openMode }; }, configurable: true }); } catch (e) {} } else { try { window.__MT_SW_FRAME__ = true; } catch (e) {} } var KEY = 'mt-global-link-window-v2'; var DEFAULTS = { height: 92, animation: 280, adaptiveColor: true, rememberHeight: true, multiTab: true, sameOriginHistory: true, mediaPreview: true, openExternal: false, openMode: 'window', autoFallback: true, newTabUrl: 'https://qq5855144.github.io/Minimal-Desktop/', swipeTab: true, swipeDistance: 90, swipeIgnore: [], blockSidePages: ['bbs.binmt.cc'], noFrame: ['github.com','gist.github.com','gitee.com','gitlab.com'], whitelist: [], blacklist: [], searchEngines: [], }; var cfg = loadConfig(); try{ if(Array.isArray(cfg.blockSidePages)){ var _hasBinmt=false,_others=[]; cfg.blockSidePages.forEach(function(x){ var s=String(x).toLowerCase(); if(s.indexOf('bbs.binmt.cc')>=0)_hasBinmt=true; else if(String(x).trim())_others.push(x); }); if(_hasBinmt){ _others.unshift('bbs.binmt.cc'); cfg.blockSidePages=_others; saveConfig(); } } }catch(e){} var state = { opened: false, tabs: [], active: -1, panel: null, mask: null, head: null, tabbar: null, content: null, track: null, floatEntry: null, floatPanel: null, color: {r:83,g:188,b:245,a:1}, noFrameHosts: {}, historyMarker: false, closingByHistory: false, drag: null }; function loadConfig() { try { return merge(DEFAULTS, JSON.parse(localStorage.getItem(KEY) || 'null') || {}); } catch (e) { return merge(DEFAULTS, {}); } } function merge(base, extra) { var out = {}; Object.keys(base).forEach(function (k) { out[k] = Array.isArray(base[k]) ? base[k].slice() : base[k]; }); Object.keys(extra || {}).forEach(function (k) { out[k] = Array.isArray(extra[k]) ? extra[k].slice() : extra[k]; }); return out; } function saveConfig() { try { localStorage.setItem(KEY, JSON.stringify(cfg)); } catch (e) {} } function hostOf(url) { try { return new URL(url, location.href).hostname.toLowerCase().replace(/^www\./, ''); } catch (e) { return ''; } } function domainMatch(host, domain) { domain = String(domain || '').toLowerCase().replace(/^https?:\/\//, '').replace(/^www\./, '').split('/')[0]; return !!domain && (host === domain || host.endsWith('.' + domain)); } function inList(host, list) { return (list || []).some(function (d) { return domainMatch(host, d); }); } function inIgnoreList(url, list){ if(!list||!list.length)return false; try{ var u=new URL(url,location.href); var key=(u.hostname+u.pathname+u.search).toLowerCase(); var keyBase=(u.hostname+u.pathname).toLowerCase(); var uq=u.search.toLowerCase(); var host=u.hostname.toLowerCase(); for(var i=0;i=0)return true; var di=d.indexOf('?'); var dBase=(di>=0?d.slice(0,di):d).replace(/\/+$/,''); if(dBase!==keyBase)continue; var a=parseQuery(di>=0?d.slice(di):''),b=parseQuery(uq),ok=true; for(var k in a){ if(k!=='view'&&(!(k in b)||b[k]!==a[k])){ok=false;break;} } if(ok)return true; } }catch(e){} return false; } function parseQuery(q){ var o={}; q.replace(/^\?/,'').split('&').forEach(function(kv){ if(!kv)return; var j=kv.indexOf('='); if(j>0)o[kv.slice(0,j)]=kv.slice(j+1); else o[kv]=''; }); return o; } function isSwipeIgnored(url){ return inIgnoreList(url, cfg.swipeIgnore); } function isNoFrameSite(url) { var h = hostOf(url); return !!h && (inList(h, cfg.noFrame) || !!state.noFrameHosts[h]); } function learnNoFrame(host) { if (!host || state.noFrameHosts[host]) return; state.noFrameHosts[host] = true; try { if (!inList(host, cfg.noFrame)) { cfg.noFrame = (cfg.noFrame || []).concat([host]); saveConfig(); } } catch (e) {} } function isWeb(url) { try { var p = new URL(url, location.href).protocol; return p === 'http:' || p === 'https:'; } catch (e) { return false; } } function shouldIntercept(a) { if (!a || !a.href || !isWeb(a.href)) return false; if (a.closest && a.closest('.mt-sw-root')) return false; if (a.hasAttribute('download')) return false; if ((a.getAttribute('rel') || '').toLowerCase().split(/\s+/).indexOf('external') >= 0) return false; var url = a.href, host = hostOf(url); if (/\.user\.(js|css)($|[?#])/i.test(url)) return false; if (inList(host, cfg.whitelist) || inList(host, cfg.blacklist)) return false; try { var u = new URL(url), c = new URL(location.href); if (u.origin === c.origin && u.pathname === c.pathname && u.search === c.search && u.hash && u.hash !== c.hash) return false; } catch (e) {} return true; } function esc(s) { return String(s == null ? '' : s).replace(/[&<>"]/g, function (c) { return {'&':'&','<':'<','>':'>','"':'"'}[c]; }); } function icon(name) { var map = { back:'', forward:'', reload:'', external:'', close:'', plus:'', settings:'' }; return '' + map[name] + ''; } function addStyle() { if (document.getElementById('mt-sw-style-v2')) return; var s = document.createElement('style'); s.id = 'mt-sw-style-v2'; s.textContent = '.mt-sw-root{position:fixed;inset:0;z-index:2147483646;pointer-events:none;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif}' + '.mt-sw-mask{position:absolute;inset:0;background:rgba(0,0,0,.45);opacity:0;transition:opacity .25s ease;pointer-events:auto}' + '.mt-sw-panel{position:absolute;left:0;right:0;bottom:0;height:var(--mt-sw-height,92vh);max-height:98vh;min-height:45vh;background:#fff;border-radius:18px 18px 0 0;overflow:hidden;display:flex;flex-direction:column;box-shadow:0 -5px 28px rgba(0,0,0,.22);transform:translateY(100%);transition:transform .28s cubic-bezier(.32,.72,.36,1);pointer-events:auto}' + '.mt-sw-show .mt-sw-mask{opacity:1}.mt-sw-show .mt-sw-panel{transform:translateY(0)}' + '.mt-sw-resize{height:7px;flex:none;cursor:ns-resize;touch-action:none;background:transparent;position:relative;z-index:4}.mt-sw-resize:after{content:"";position:absolute;left:50%;top:3px;width:42px;height:4px;border-radius:4px;transform:translateX(-50%);background:rgba(0,0,0,.18)}' + '.mt-sw-head{display:flex;align-items:center;gap:5px;padding:4px 8px;background:var(--mt-sw-color,#53BCF5);color:var(--mt-sw-fg,#fff);transition:background .22s ease,color .22s ease;flex:none}' + '.mt-sw-nav{display:flex;gap:4px;flex:none}.mt-sw-btn{width:32px;height:32px;border:0;border-radius:50%;padding:0;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.2);color:inherit;cursor:pointer}.mt-sw-btn:active{transform:scale(.94)}.mt-sw-btn:disabled{opacity:.38}' + '.mt-sw-title{min-width:0;flex:1;font-size:13px;font-weight:600;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;padding:0 4px}' + '.mt-sw-tabbar{display:flex;align-items:center;gap:4px;padding:4px 7px;background:var(--mt-sw-color,#53BCF5);color:var(--mt-sw-fg,#fff);border-bottom:1px solid rgba(255,255,255,.24);overflow-x:auto;scrollbar-width:none;flex:none;transition:background .22s ease,color .22s ease}.mt-sw-tabbar::-webkit-scrollbar{display:none}' + '.mt-sw-tab{height:30px;min-width:64px;max-width:76px;display:flex;align-items:center;gap:3px;padding:0 7px;border:0;border-radius:8px;background:rgba(255,255,255,.18);font-size:12px;color:inherit;flex:none;cursor:pointer;transition:background .18s ease,color .18s ease}.mt-sw-tab.active{background:rgba(255,255,255,.92);color:#222;box-shadow:none}.mt-sw-tab span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap;flex:1;text-align:center}.mt-sw-tab b{font-weight:400;font-size:15px;line-height:1;color:inherit;opacity:.72}.mt-sw-add{width:30px;height:30px;border:0;border-radius:8px;background:rgba(255,255,255,.14);color:inherit;flex:none}' + '.mt-sw-content{position:relative;flex:1;min-height:0;background:#fff}.mt-sw-frame{position:absolute;inset:0;width:100%;height:100%;border:0;background:#fff}.mt-sw-media{position:absolute;inset:0;width:100%;height:100%;object-fit:contain;background:#111}.mt-sw-audio{position:absolute;left:16px;right:16px;top:50%;transform:translateY(-50%);width:calc(100% - 32px)}' + '.mt-sw-error{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:12px;color:#666;background:#fff;padding:24px;text-align:center}.mt-sw-error button{border:0;border-radius:9px;padding:9px 14px;background:#eee}' + '.mt-sw-settings{position:absolute;inset:0;background:rgba(255,255,255,.98);z-index:8;overflow:auto;padding:18px;display:none}.mt-sw-settings.show{display:block}.mt-sw-settings h3{margin:0 0 16px;font-size:18px}.mt-sw-setting{padding:12px 0;border-bottom:1px solid #eee}.mt-sw-setting label{display:flex;align-items:center;justify-content:space-between;gap:12px;font-size:14px}.mt-sw-setting input[type=number]{width:76px}.mt-sw-setting select{font-size:14px;padding:6px 8px;border:1px solid #ddd;border-radius:8px;background:#fff;color:#333}.mt-sw-setting textarea{width:100%;box-sizing:border-box;min-height:70px;margin-top:8px;border:1px solid #ddd;border-radius:8px;padding:8px;resize:vertical}.mt-sw-settings-actions{display:flex;gap:8px;margin-top:16px}.mt-sw-settings-actions button{flex:1;border:0;border-radius:9px;padding:10px}.mt-sw-primary{background:#53BCF5;color:#fff}.mt-sw-secondary{background:#eee}.mt-sw-tip{font-size:11px;color:#999;line-height:1.5;margin-top:5px}'; (document.head || document.documentElement).appendChild(s); var tv = document.createElement('style'); tv.textContent = '.mt-sw-tab{background:var(--mt-sw-tab-bg,rgba(255,255,255,.18));color:var(--mt-sw-tab-fg,#fff)}.mt-sw-tab.active{background:var(--mt-sw-tab-active-bg,rgba(255,255,255,.92));color:var(--mt-sw-tab-active-fg,#222)}.mt-sw-tab b{opacity:.72}.mt-sw-add{background:var(--mt-sw-add-bg,rgba(255,255,255,.14));color:var(--mt-sw-tab-fg,#fff)}.mt-sw-btn{background:var(--mt-sw-btn-bg,rgba(255,255,255,.2))}.mt-sw-tabbar{border-bottom-color:var(--mt-sw-tabline,rgba(255,255,255,.24))}'; (document.head || document.documentElement).appendChild(tv); var tw = document.createElement('style'); tw.textContent = '.mt-sw-track{position:absolute;inset:0;display:flex;height:100%;will-change:transform;transition:transform .34s cubic-bezier(.22,.68,.28,1)}.mt-sw-track>.mt-sw-frame,.mt-sw-track>.mt-sw-media,.mt-sw-track>.mt-sw-audio,.mt-sw-track>.mt-sw-error{position:relative;inset:auto;left:auto;right:auto;top:auto;bottom:auto;flex:0 0 100%;width:100%;min-width:100%;height:100%;margin:0}.mt-sw-track>.mt-sw-audio{display:flex;align-items:center;justify-content:center}.mt-sw-track>.mt-sw-audio{width:100%}.mt-sw-track>.mt-sw-error{display:flex}'; (document.head || document.documentElement).appendChild(tw); var needCssom = false; try { needCssom = !s.sheet || !s.sheet.cssRules || s.sheet.cssRules.length === 0; } catch (e) { needCssom = true; } if (needCssom) { try { var css = s.textContent; s.textContent = ''; var parts = css.split('}'); for (var i = 0; i < parts.length; i++) { var rule = parts[i].trim(); if (rule && s.sheet) s.sheet.insertRule(rule + '}', s.sheet.cssRules.length); } } catch (e) {} } } function mediaType(url) { if (!cfg.mediaPreview) return 'html'; var p = ''; try { p = new URL(url).pathname.toLowerCase(); } catch (e) {} if (/\.(png|jpe?g|gif|webp|bmp|svg|avif|ico)(?:$|\?)/i.test(p)) return 'image'; if (/\.(mp4|webm|m4v|mov|ogv)(?:$|\?)/i.test(p)) return 'video'; if (/\.(mp3|m4a|aac|ogg|wav|flac)(?:$|\?)/i.test(p)) return 'audio'; if (/\.pdf(?:$|\?)/i.test(p)) return 'pdf'; return 'html'; } function createTab(url, title, activate) { if (activate !== false && state.active >= 0 && state.tabs[state.active]) { saveScroll(state.tabs[state.active]); } var t = {id:Date.now().toString(36)+Math.random().toString(36).slice(2,7),url:url,title:title||hostOf(url)||'网页',history:[url],historyIndex:0,view:null,frame:null,element:null,loading:true,sameOrigin:false,fallbackTried:false,bridged:false,scrollY:0,scrollX:0}; state.tabs.push(t); if (activate !== false) state.active=state.tabs.length-1; return t; } function activeTab() { return state.tabs[state.active] || null; } function setPanelHeight(percent) { if(!state.panel)return; percent=Math.max(45,Math.min(98,Number(percent)||92)); state.panel.style.setProperty('--mt-sw-height',percent+'vh'); state.panel.dataset.height=percent; } // 全局高度记忆:高度直接写入 cfg.height 并持久化(不再按站点区分) function getSiteColor(host){ try { var s=JSON.parse(localStorage.getItem(KEY+':sites')||'{}'); return s[host]&&s[host].color; } catch(e){return null;} } function saveSiteColor(host,c){ if(!host||!c)return; try{ var s=JSON.parse(localStorage.getItem(KEY+':sites')||'{}'); var e=s[host]||{};e.color={r:c.r,g:c.g,b:c.b}; s[host]=e;localStorage.setItem(KEY+':sites',JSON.stringify(s)); }catch(err){} } function luma(c){return(c.r*299+c.g*587+c.b*114)/1000;} function parseColor(v){ if(!v||v==='transparent')return null; var m=String(v).match(/^rgba?\(\s*(\d+)\D+(\d+)\D+(\d+)(?:\D+([\d.]+))?\s*\)$/i); if(m&&(m[4]==null||parseFloat(m[4])>.05))return{r:+m[1],g:+m[2],b:+m[3],a:m[4]==null?1:+m[4]}; m=String(v).match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i); if(m){var h=m[1];if(h.length===3)h=h.split('').map(function(x){return x+x}).join('');return{r:parseInt(h.slice(0,2),16),g:parseInt(h.slice(2,4),16),b:parseInt(h.slice(4,6),16),a:1};} return null; } function gradientColor(v){ if(!v||v==='none')return null; var m=String(v).match(/(?:linear|radial)-gradient\([^)]*?((?:#[0-9a-f]{3,6}|rgba?\([^)]*\)))/i); return m?parseColor(m[1]):null; } function colorDistance(a,b){return Math.sqrt(Math.pow(a.r-b.r,2)+Math.pow(a.g-b.g,2)+Math.pow(a.b-b.b,2));} function detectTopColor(t,done){ var fallback={r:83,g:188,b:245,a:1}; try{ var doc=t.frame&&t.frame.contentDocument, win=t.frame&&t.frame.contentWindow; if(!doc||!win)throw 0; var meta=doc.querySelector('meta[name="theme-color"],meta[name="msapplication-navbutton-color"]'); var mc=meta&&parseColor(meta.content); if(mc && (luma(mc)<245 || colorDistance(mc,{r:255,g:255,b:255})>18)) return done(mc); var candidates=[]; var selectors=[ 'header','nav','[role="banner"]', '.header','.navbar','.nav','.topbar','.top-bar','.site-header','.page-header', '.header-wrap','.header-inner','.top-header','.main-header', '.head','.headbar','.top','.topnav','.menu','.menu-bar' ]; selectors.forEach(function(sel){ try{Array.prototype.slice.call(doc.querySelectorAll(sel)).slice(0,12).forEach(function(el){candidates.push(el);});}catch(e){} }); try{ Array.prototype.slice.call(doc.body.querySelectorAll('*')).slice(0,800).forEach(function(el){ var r=el.getBoundingClientRect(); if(r.top<=80 && r.bottom>=20 && r.width>=Math.min(win.innerWidth*.55,360) && r.height>=24 && r.height<=180) candidates.push(el); }); }catch(e){} var best=null,bestScore=-Infinity,seen=[]; candidates.forEach(function(el){ if(seen.indexOf(el)>=0)return; seen.push(el); try{ var r=el.getBoundingClientRect(); if(!r||r.width140||r.bottom<0)return; var cs=win.getComputedStyle(el); var c=parseColor(cs.backgroundColor)||gradientColor(cs.backgroundImage); if(!c)return; var area=Math.min(r.width,win.innerWidth)*Math.min(r.height,180); var topBonus=Math.max(0,100-Math.max(0,r.top))*3; var heightBonus=Math.min(r.height,100); var nonWhiteBonus=luma(c)<238 ? 260 : 0; var score=area*.003+topBonus+heightBonus+nonWhiteBonus; if(luma(c)>248 && r.height>120) score-=500; if(score>bestScore){bestScore=score;best=c;} }catch(e){} }); if(best)return done(best); var bodyBg=null; try{ var bcs=win.getComputedStyle(doc.body); bodyBg=parseColor(bcs.backgroundColor)||gradientColor(bcs.backgroundImage); }catch(e){} if(bodyBg&&luma(bodyBg)>238)return done({r:248,g:248,b:248,a:1}); done(fallback); }catch(e){done(fallback);} } function detectColor(t,done){ detectTopColor(t,function(c){ done(c); }); } function applyColor(t){ if(!state.head||!cfg.adaptiveColor)return; if(t&&t.view&&(t.view.tagName==='IMG'||t.view.tagName==='VIDEO'||t.view.tagName==='AUDIO')){ applyThemeVars({r:34,g:34,b:34,a:1}); return; } detectColor(t,function(c){ state.color=c; applyThemeVars(c); var host=hostOf(t&&t.url); if(host)saveSiteColor(host,c); }); } function applyThemeVars(c){ if(!state.head||!state.panel)return; var dark=luma(c)<=205; var vars={ '--mt-sw-color':'rgb('+c.r+','+c.g+','+c.b+')', '--mt-sw-fg':dark?'#fff':'rgba(0,0,0,.7)', '--mt-sw-tab-bg':dark?'rgba(255,255,255,.18)':'rgba(0,0,0,.07)', '--mt-sw-tab-fg':dark?'rgba(255,255,255,.92)':'rgba(0,0,0,.62)', '--mt-sw-tab-active-bg':dark?'rgba(255,255,255,.92)':'#ffffff', '--mt-sw-tab-active-fg':dark?'#222':'#111', '--mt-sw-add-bg':dark?'rgba(255,255,255,.14)':'rgba(0,0,0,.06)', '--mt-sw-btn-bg':dark?'rgba(255,255,255,.2)':'rgba(0,0,0,.08)', '--mt-sw-tabline':dark?'rgba(255,255,255,.24)':'rgba(0,0,0,.08)' }; Object.keys(vars).forEach(function(k){ state.head.style.setProperty(k,vars[k]); state.panel.style.setProperty(k,vars[k]); }); } function retryColor(t){ if(!cfg.adaptiveColor||!t||!t.frame)return; [80,260,700,1400].forEach(function(delay){ setTimeout(function(){ if(state.opened&&activeTab()===t&&!t.frame)return; if(state.opened&&activeTab()===t)applyColor(t); },delay); }); } function updateNav(){ var t=activeTab(),back=state.panel&&state.panel.querySelector('[data-act=back]'),fwd=state.panel&&state.panel.querySelector('[data-act=forward]'); if(back)back.disabled=!t||t.historyIndex<=0; if(fwd)fwd.disabled=!t||t.historyIndex>=t.history.length-1; var title=state.panel&&state.panel.querySelector('.mt-sw-title'); if(title)title.textContent=t?t.title:'网页小窗'; } function tabLabel(title){ var text=String(title||'网页').replace(/\s+/g,' ').trim(); var chars=Array.from(text); return chars.length>4?chars.slice(0,4).join('')+'…':chars.join(''); } function updateTabs(){ if(!state.tabbar)return; state.tabbar.innerHTML=''; state.tabs.forEach(function(t,i){ var b=document.createElement('button');b.className='mt-sw-tab'+(i===state.active?' active':'');b.title=t.title||'网页'; b.innerHTML=''+esc(tabLabel(t.title))+'×'; b.onclick=function(e){var hitB=e.target&&e.target.closest?e.target.closest('b'):null;if(hitB===b.querySelector('b'))closeTab(i);else activateTab(i);}; state.tabbar.appendChild(b); }); var add=document.createElement('button');add.className='mt-sw-add';add.innerHTML=icon('plus');add.title='新标签页'; add.onclick=function(){var nt=createTab(cfg.newTabUrl||'about:blank','新标签页');renderTabContent(nt);renderTabs();}; state.tabbar.appendChild(add); updateNav(); } function activateTab(i){ if(i<0||i>=state.tabs.length)return; if(state.active>=0&&state.active!==i)saveScroll(state.tabs[state.active]); state.active=i; renderTabs(); var t=activeTab(); if(t){ restoreScroll(t); [50,250].forEach(function(d){ setTimeout(function(){ if(state.opened&&activeTab()===t)restoreScroll(t); },d); }); applyColor(t); } } function saveScroll(t){ if(!t||!t.frame)return; try{ var d=t.frame.contentDocument; if(d&&d.scrollingElement){ t.scrollY=d.scrollingElement.scrollTop; t.scrollX=d.scrollingElement.scrollLeft; } }catch(e){} } function restoreScroll(t){ if(!t||!t.frame||(!t.scrollY&&!t.scrollX))return; try{ var d=t.frame.contentDocument; if(d&&d.scrollingElement){ d.scrollingElement.scrollTop=t.scrollY; d.scrollingElement.scrollLeft=t.scrollX; } }catch(e){} } function closeTab(i){ var t=state.tabs[i];if(!t)return; if(t.element)t.element.remove(); state.tabs.splice(i,1); if(!state.tabs.length){ var nt=createTab(cfg.newTabUrl||'about:blank','新标签页'); renderTabContent(nt);renderTabs(); return; } if(state.active>i)state.active--;else if(state.active>=state.tabs.length)state.active=state.tabs.length-1; activateTab(state.active); } function renderTabs(){ if(state.track){ state.track.style.transition='transform .34s cubic-bezier(.22,.68,.28,1)'; state.track.style.transform='translateX(-'+(state.active*100)+'%)'; } updateTabs(); } function pushHistory(t,url){ if(!url||url==='about:blank')return; if(t.history[t.historyIndex]===url)return; t.history=t.history.slice(0,t.historyIndex+1);t.history.push(url);t.historyIndex=t.history.length-1; } function navigateTab(t,url,addHistory){ if(!t)return; if(addHistory)pushHistory(t,url); t.url=url;t.loading=true;t.frame=null;t.fallbackTried=false;t.bridged=false;t.scrollY=0;t.scrollX=0; if(t.view){t.view.remove();t.view=null;} renderTabContent(t);updateNav(); } function placeView(t,el){ var track=state.track; if(!track){ if(state.content)state.content.appendChild(el); return; } var idx=state.tabs.indexOf(t); var anchor=(idx>=0)?track.children[idx]:null; if(anchor)track.insertBefore(el,anchor); else track.appendChild(el); } function renderTabContent(t){ if(!state.content)return; var type=mediaType(t.url),el; if(type==='image'){ el=document.createElement('img');el.className='mt-sw-media';el.src=t.url;el.onerror=function(){showTabError(t,'图片无法加载');}; }else if(type==='video'){ el=document.createElement('video');el.className='mt-sw-media';el.controls=true;el.playsInline=true;el.src=t.url;el.onerror=function(){showTabError(t,'视频无法播放');}; }else if(type==='audio'){ el=document.createElement('audio');el.className='mt-sw-audio';el.controls=true;el.src=t.url;el.onerror=function(){showTabError(t,'音频无法播放');}; }else{ el=document.createElement('iframe');el.className='mt-sw-frame';el.src=t.url;el.setAttribute('loading','eager'); el.setAttribute('allowfullscreen',''); el.setAttribute('allow','accelerometer; autoplay; camera; clipboard-read; clipboard-write; display-capture; encrypted-media; fullscreen; geolocation; gyroscope; microphone; midi; payment; picture-in-picture; screen-wake-lock; usb; web-share; xr-spatial-tracking'); t.frame=el; if(cfg.adaptiveColor){ applyThemeVars(getSiteColor(hostOf(t.url))||{r:83,g:188,b:245,a:1}); } el.addEventListener('load',function(){ t.loading=false; try{t.sameOrigin=!!el.contentDocument;}catch(e){t.sameOrigin=false;} if(t.sameOrigin){ try{ var doc=el.contentDocument; var cur=doc&&doc.location.href; if(cur&&cur!==t.url&&cur!=='about:blank'){pushHistory(t,cur);t.url=cur;} if(doc&&doc.title){t.title=String(doc.title).slice(0,80);} }catch(e){} try{ var gd=el.contentDocument; if(gd){ try{gd.documentElement.style.touchAction=(cfg.swipeTab&&(!isSwipeIgnored(t.url)||inIgnoreList(t.url,cfg.blockSidePages)))?'pan-y':'auto';}catch(err){} if(!gd.__MT_SW_GEST__){ gd.__MT_SW_GEST__=true; attachSwipeTo(gd,function(dir){ var at=activeTab(); if(at&&isSwipeIgnored(at.url)&&!inIgnoreList(at.url,cfg.blockSidePages))return; swipeTab(dir); },null); } if(inIgnoreList(t.url,cfg.blockSidePages)){ blockPageRightSwipe(gd); setTimeout(function(){try{blockPageRightSwipe(gd);}catch(e){}},700); setTimeout(function(){try{blockPageRightSwipe(gd);}catch(e){}},2500); } } }catch(e){} } applyColor(t);retryColor(t);installSameOriginBridge(t); restoreScroll(t); updateTabs(); }); el.addEventListener('error',function(){ t.frame=null; learnNoFrame(hostOf(t.url)); var autoOpened=false; if(cfg.autoFallback&&!t.fallbackTried){ t.fallbackTried=true; try{autoOpened=!!window.open(t.url,'_blank','noopener');}catch(e){} } showTabError(t,'该网站禁止被嵌入小窗(X-Frame-Options/CSP)。'+(autoOpened?'已在新标签页打开,此后该站点链接将直接新标签页打开。':'点击下方按钮用新标签页打开,此后该站点链接将直接新标签页打开。')); }); } t.view=el;t.element=el;placeView(t,el); renderTabs(); } function showTabError(t,msg){ if(t.frame){t.frame=null;} var box=document.createElement('div');box.className='mt-sw-error';box.innerHTML='
'+esc(msg)+'
'; box.querySelector('button').onclick=function(){window.open(t.url,'_blank','noopener');}; if(t.element)t.element.remove();t.view=box;t.element=box;placeView(t,box);renderTabs(); } function swipeTab(delta){ if(!cfg.swipeTab||state.tabs.length<2)return; var n=state.active+delta; if(n<0)n=state.tabs.length-1; if(n>=state.tabs.length)n=0; activateTab(n); } function attachSwipeTo(root,onSwipe,skipSelector){ var sx=null,sy=null,fired=false; root.addEventListener('pointerdown',function(e){ if(skipSelector&&e.target&&e.target.closest&&e.target.closest(skipSelector))return; sx=e.clientX;sy=e.clientY;fired=false; },true); root.addEventListener('pointermove',function(e){ if(sx==null||fired)return; var dx=e.clientX-sx,dy=e.clientY-sy,adx=Math.abs(dx),ady=Math.abs(dy); var dist=cfg.swipeDistance||90; if(adxadx*0.35)return; fired=true; onSwipe(dx<0?1:-1); },true); function reset(){sx=null;sy=null;} root.addEventListener('pointerup',reset,true); root.addEventListener('pointercancel',reset,true); } function blockPageRightSwipe(doc){ try{ if(!doc||!doc.defaultView)return; var w=doc.defaultView; if(!doc.__MT_SW_BLOCKSIDE__){ doc.__MT_SW_BLOCKSIDE__=true; try{ var st=doc.createElement('style'); st.setAttribute('data-mt-sw-sideblock','1'); st.textContent='.comiis_sidenv_box,.comiis_leftmenubg{display:none!important;} body{overflow:visible!important;height:auto!important;width:auto!important;}'; (doc.head||doc.documentElement).appendChild(st); }catch(e){} } try{ var noop=function(){}; w.comiis_leftnv=noop; try{ Object.defineProperty(w,'comiis_leftnv',{configurable:true,enumerable:true,get:function(){return noop;},set:function(v){}}); }catch(e){} }catch(e){} if(!doc.__MT_SW_SIDE_MO__){ doc.__MT_SW_SIDE_MO__=true; try{ var mo=new w.MutationObserver(function(){ try{ var body=doc.querySelector('.comiis_body'); if(body&&body.classList.contains('comiis_showleftnv')){ body.classList.remove('comiis_showleftnv'); body.classList.add('comiis_hideleftnv'); } if(body&&body.style.overflow==='hidden')body.style.overflow=''; if(doc.body&&doc.body.style.overflow==='hidden')doc.body.style.overflow=''; var box=doc.querySelector('.comiis_sidenv_box'),bg=doc.querySelector('.comiis_leftmenubg'); if(box&&box.style.display==='block')box.style.display='none'; if(bg&&bg.style.display==='block')bg.style.display='none'; }catch(e){} }); mo.observe(doc.documentElement,{attributes:true,subtree:true,attributeFilter:['class','style']}); }catch(e){} } try{ var b2=doc.querySelector('.comiis_body'); if(b2&&b2.classList.contains('comiis_showleftnv')){b2.classList.remove('comiis_showleftnv');b2.classList.add('comiis_hideleftnv');} if(doc.body&&doc.body.style.overflow==='hidden')doc.body.style.overflow=''; var bx=doc.querySelector('.comiis_sidenv_box'),bg2=doc.querySelector('.comiis_leftmenubg'); if(bx&&bx.style.display==='block')bx.style.display='none'; if(bg2&&bg2.style.display==='block')bg2.style.display='none'; }catch(e){} }catch(e){} } function installSameOriginBridge(t){ if(!cfg.sameOriginHistory||!t.frame)return; try{ var doc=t.frame.contentDocument; if(!doc||doc.__MT_SW_BRIDGED__)return; doc.__MT_SW_BRIDGED__=true; doc.addEventListener('click',function(e){ if(e.defaultPrevented||e.button!==0||e.ctrlKey||e.metaKey||e.shiftKey||e.altKey)return; var a=e.target&&e.target.closest?e.target.closest('a[href]'):null; if(!a||!isWeb(a.href)||a.hasAttribute('download'))return; var h=hostOf(a.href); if(inList(h,cfg.whitelist)||inList(h,cfg.blacklist))return; try{ var current=new URL(t.url),next=new URL(a.href,doc.location.href); if(next.origin===current.origin&&next.pathname===current.pathname&&next.search===current.search&&next.hash&&next.hash!==current.hash)return; }catch(err){} e.preventDefault();e.stopPropagation(); if(cfg.multiTab){ var nt=createTab(a.href,titleFromAnchor(a),true); renderTabContent(nt);updateTabs(); }else{ navigateTab(t,a.href,true); } },true); }catch(e){} } function openTab(url,title){ if(!isWeb(url))return; if(isNoFrameSite(url)){try{window.open(url,'_blank','noopener');}catch(e){}return;} if(!state.opened)openPanel(url); var t=createTab(url,title,true); if(cfg.multiTab||state.tabs.length===1)renderTabContent(t); else{ var old=state.tabs[state.tabs.length-2]; navigateTab(old,url,true);state.tabs.pop(); } updateTabs(); } function openPanel(initialUrl){ if(state.opened)return; addStyle();state.opened=true;state.tabs=[];state.active=-1; var root=document.createElement('div');root.className='mt-sw-root'; var mask=document.createElement('div');mask.className='mt-sw-mask'; var panel=document.createElement('div');panel.className='mt-sw-panel'; var resize=document.createElement('div');resize.className='mt-sw-resize';resize.title='拖动调整小窗高度'; var head=document.createElement('div');head.className='mt-sw-head'; var nav=document.createElement('div');nav.className='mt-sw-nav'; function btn(act,title,ic){var b=document.createElement('button');b.className='mt-sw-btn';b.dataset.act=act;b.title=title;b.innerHTML=icon(ic);return b;} var back=btn('back','后退','back'),forward=btn('forward','前进','forward'),reload=btn('reload','刷新','reload');nav.append(back,forward,reload); var title=document.createElement('div');title.className='mt-sw-title';title.textContent='网页小窗'; var actions=document.createElement('div');actions.className='mt-sw-nav'; var settings=btn('settings','设置','settings'),external=btn('external','新窗口打开','external'),close=btn('close','关闭','close');actions.append(settings,external,close);head.append(nav,title,actions); var tabbar=document.createElement('div');tabbar.className='mt-sw-tabbar'; var content=document.createElement('div');content.className='mt-sw-content'; var settingsBox=document.createElement('div');settingsBox.className='mt-sw-settings'; panel.append(resize,head,tabbar,content,settingsBox);root.append(mask,panel);document.body.appendChild(root); state.panel=panel;state.mask=mask;state.head=head;state.tabbar=tabbar;state.content=content; var track=document.createElement('div');track.className='mt-sw-track'; content.appendChild(track); content.style.overflow='hidden'; state.track=track; setPanelHeight(cfg.height); back.onclick=function(){goHistory(-1);};forward.onclick=function(){goHistory(1);}; reload.onclick=function(){var t=activeTab();if(t){if(t.frame)t.frame.src=t.url;else navigateTab(t,t.url,false);}}; external.onclick=function(){var t=activeTab();if(t)window.open(t.url,'_blank','noopener');}; close.onclick=function(){closePanel(true);};mask.onclick=function(){closePanel(true);};settings.onclick=function(){toggleSettings(true);}; head.style.touchAction='none'; attachSwipeTo(head,swipeTab,'.mt-sw-btn'); resize.addEventListener('pointerdown',function(e){e.preventDefault();state.drag={startY:e.clientY,startHeight:panel.getBoundingClientRect().height};resize.setPointerCapture&&resize.setPointerCapture(e.pointerId);}); resize.addEventListener('pointermove',function(e){if(!state.drag)return;var h=state.drag.startHeight+(state.drag.startY-e.clientY);setPanelHeight(Math.max(45,Math.min(98,h/window.innerHeight*100)));}); resize.addEventListener('pointerup',function(){if(!state.drag)return;if(cfg.rememberHeight){cfg.height=Math.round(parseFloat(panel.dataset.height)||92);saveConfig();}state.drag=null;}); buildSettings(settingsBox); updateTabs();requestAnimationFrame(function(){root.classList.add('mt-sw-show');}); if(!state.historyMarker){try{history.pushState({mtGlobalWindow:true},'',location.href);state.historyMarker=true;}catch(e){}} } function closePanel(useHistory){ if(!state.opened)return; if(useHistory&&state.historyMarker&&!state.closingByHistory){ state.closingByHistory=true; setTimeout(function(){ if(state.opened)finishClose(); },300); try{history.back();}catch(e){finishClose();} return; } finishClose(); } function finishClose(){ state.tabs.forEach(function(t){if(t.element)t.element.remove();});state.tabs=[];state.active=-1;state.opened=false; if(state.panel&&state.panel.parentNode)state.panel.parentNode.parentNode.removeChild(state.panel.parentNode); state.panel=state.mask=state.head=state.tabbar=state.content=state.track=null;state.historyMarker=false;state.closingByHistory=false; } function goHistory(delta){ var t=activeTab();if(!t)return;var next=t.historyIndex+delta;if(next<0||next>=t.history.length)return; t.historyIndex=next;t.url=t.history[next];renderTabContent(t);updateTabs(); } function toggleSettings(show){var box=state.panel&&state.panel.querySelector('.mt-sw-settings');if(box)box.classList.toggle('show',show);} function ensureStandaloneEntry(){ if(cfg.openMode!=='tab'){ if(state.floatEntry){state.floatEntry.remove();state.floatEntry=null;} if(state.floatPanel){state.floatPanel.remove();state.floatPanel=null;} return; } if(state.floatEntry||FRAME_MODE)return; var btn=document.createElement('div'); btn.title='全站链接小窗:设置(当前为新标签页模式,点击可改回小窗模式)'; btn.textContent='\u2699'; btn.style.cssText='position:fixed;right:14px;bottom:120px;width:40px;height:40px;border-radius:50%;background:rgba(83,188,245,.72);color:#fff;font-size:22px;line-height:40px;text-align:center;z-index:2147483645;box-shadow:0 2px 10px rgba(0,0,0,.3);cursor:pointer;user-select:none;touch-action:none'; btn.onclick=function(e){e.preventDefault();e.stopPropagation();toggleFloatSettings();}; document.body.appendChild(btn); state.floatEntry=btn; } function toggleFloatSettings(){ if(state.floatPanel){state.floatPanel.remove();state.floatPanel=null;return;} var wrap=document.createElement('div'); wrap.style.cssText='position:fixed;right:10px;bottom:172px;width:min(320px,calc(100vw - 20px));max-height:62vh;overflow:auto;background:#fff;border-radius:12px;box-shadow:0 8px 28px rgba(0,0,0,.32);z-index:2147483646;padding:14px;box-sizing:border-box;font-size:14px;color:#222;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif'; var box=document.createElement('div');box.className='mt-sw-settings'; box.style.cssText='position:static;display:block;background:transparent;padding:0;overflow:visible;z-index:auto'; wrap.appendChild(box); document.body.appendChild(wrap); state.floatPanel=wrap; buildSettings(box); var c=box.querySelector('[data-cancel]'); if(c)c.onclick=function(){toggleFloatSettings();}; } function afterSettingsSaved(){ if(state.floatPanel){state.floatPanel.remove();state.floatPanel=null;} ensureStandaloneEntry(); } function buildSettings(box){ box.innerHTML='

小窗设置

'+ '
'+ '
同源页面优先读取 theme-color、顶部导航、渐变背景;跨域 iframe 无法读取 DOM 时自动使用安全兜底色。
'+ '
拖动调整小窗高度后记住,下次打开小窗沿用(全局生效,不区分网站);关闭后每次按「默认高度」打开。
'+ '
'+ '
图片、视频、音频、PDF 优先使用对应预览器。
'+ '
新标签页模式下已安装的用户脚本 100% 生效;小窗模式依赖脚本管理器允许向 iframe 注入脚本(见下方说明)。
'+ '
部分网站通过 X-Frame-Options / CSP frame-ancestors 禁止被嵌入 iframe,开启后自动改用新标签页打开(可能被浏览器弹窗拦截)。
'+ '
关于用户脚本生效
Tampermonkey / Violentmonkey 等管理器默认会向小窗内的 iframe 注入用户脚本,且本小窗 iframe 未加 sandbox,已开启全屏、剪贴板、自动播放等权限。若某个脚本在小窗中不生效,通常是该脚本声明了 @noframes 或检测 top!==self 主动退出,本脚本无法代为修改;此类情况请点击顶栏 ⧉ 按钮或改用「新标签页模式」打开。
'+ '
白名单域名
白名单网站的链接保持正常浏览器打开。
'+ '
黑名单域名
黑名单网站完全不进入小窗。
'+ '
'; (function(){ var bl=box.querySelector('[data-set=blacklist]'); if(bl){ bl.closest('.mt-sw-setting').insertAdjacentHTML('afterend', '
禁嵌小窗站点
这些站点禁止被 iframe 嵌入(如 GitHub),其链接将直接在新标签页打开;加载失败的站点也会自动加入此列表。
'); } var mr=box.querySelector('[data-set=multiTab]'); if(mr){ mr.closest('.mt-sw-setting').insertAdjacentHTML('afterend', '
在标题栏或小窗内容上横向滑动约90px(需接近水平,小幅度滑动不触发)循环切换标签页;小窗打开期间屏蔽浏览器横滑手势,关闭后自动恢复。注意:小窗内页面依赖原生横向滚动的容器(部分轮播/横向表格)将不再响应横滑,可点顶栏 ⧉ 在新窗口打开浏览。
'); } var sr=box.querySelector('[data-set=swipeTab]'); if(sr){ sr.closest('.mt-sw-setting').insertAdjacentHTML('afterend', '
手势禁用页面
这些页面在小窗内禁用横滑切换标签手势,页面自身横滑交互(右滑侧边栏等)正常;标题栏手势仍可用。填「主机+路径+参数」片段,每行一个。
'+ '
禁用页面侧边栏
这些页面在小窗内完全屏蔽页面自身的右滑侧边栏(右滑只用于切换标签页),纵向滚动、点击、左滑不受影响;关闭小窗后页面自动恢复原状。填纯域名(如 bbs.binmt.cc)则该站全部页面生效;也可填「主机+路径+参数」精确到页面(比实际地址少带参数也能命中)。仅对同源页面/脚本管理器可注入 iframe 的环境生效。
'); } })(); box.querySelector('[data-cancel]').onclick=function(){toggleSettings(false);}; box.querySelector('[data-save]').onclick=function(){ cfg.height=Math.max(45,Math.min(98,Number(box.querySelector('[data-set=height]').value)||92)); ['adaptiveColor','rememberHeight','multiTab','mediaPreview','autoFallback','swipeTab'].forEach(function(k){cfg[k]=box.querySelector('[data-set='+k+']').checked;}); cfg.openMode=box.querySelector('[data-set=openMode]').value==='tab'?'tab':'window'; ['whitelist','blacklist','noFrame'].forEach(function(k){cfg[k]=box.querySelector('[data-set='+k+']').value.split(/\r?\n|,/).map(function(x){return x.trim().toLowerCase();}).filter(Boolean);}); cfg.swipeIgnore=box.querySelector('[data-set=swipeIgnore]').value.split(/\r?\n|,/).map(function(x){return x.trim();}).filter(Boolean); cfg.blockSidePages=box.querySelector('[data-set=blockSidePages]').value.split(/\r?\n|,/).map(function(x){return x.trim();}).filter(Boolean); saveConfig();setPanelHeight(cfg.height);toggleSettings(false);var t=activeTab();if(t)applyColor(t);afterSettingsSaved(); }; } function titleFromAnchor(a){return(a.getAttribute('title')||a.getAttribute('aria-label')||a.textContent||hostOf(a.href)||'网页').replace(/\s+/g,' ').trim().slice(0,80);} function postFrameLink(url,title){ try{ window.top.postMessage({ type:'MT_GLOBAL_LINK_WINDOW_OPEN', url:url, title:title||hostOf(url)||'网页' },'*'); }catch(e){} } var frameBridge = { ready: false, cfg: null }; function initFrameMode(){ try{ if(window.top && window.top.__MT_SW_PANEL_HOST__){ frameBridge.ready = true; frameBridge.cfg = window.top.__MT_SW_PANEL_HOST__; } }catch(e){} function hello(){ if(frameBridge.ready)return; try{ window.top.postMessage({type:'MT_GLOBAL_LINK_WINDOW_HELLO',url:location.href,title:document.title}, '*'); }catch(e){} } hello(); [300,1000,2500,5000].forEach(function(d){ setTimeout(hello, d); }); window.addEventListener('message',function(e){ var d=e.data; if(!d||d.type!=='MT_GLOBAL_LINK_WINDOW_CFG')return; try{ if(e.source!==window.top)return; }catch(err){return;} frameBridge.ready=true; frameBridge.cfg=d.cfg||{}; try{ if(frameBridge.cfg.swipeTab&&(!inIgnoreList(location.href,frameBridge.cfg.swipeIgnore)||inIgnoreList(location.href,frameBridge.cfg.blockSidePages)))document.documentElement.style.touchAction='pan-y'; }catch(err){} if(inIgnoreList(location.href,frameBridge.cfg.blockSidePages)){ blockPageRightSwipe(document); setTimeout(function(){try{blockPageRightSwipe(document);}catch(e){}},700); setTimeout(function(){try{blockPageRightSwipe(document);}catch(e){}},2500); } }); attachSwipeTo(document,function(dir){ if(!frameBridge.ready)return; if(inIgnoreList(location.href,frameBridge.cfg&&frameBridge.cfg.swipeIgnore)&&!inIgnoreList(location.href,frameBridge.cfg&&frameBridge.cfg.blockSidePages))return; try{ window.top.postMessage({type:'MT_GLOBAL_LINK_WINDOW_SWIPE',dir:dir}, '*'); }catch(e){} },null); document.addEventListener('click',function(e){ if(!frameBridge.ready)return; if(e.defaultPrevented||e.button!==0||e.ctrlKey||e.metaKey||e.shiftKey||e.altKey)return; var a=e.target&&e.target.closest?e.target.closest('a[href]'):null; if(!shouldIntercept(a))return; var fcfg=frameBridge.cfg; if(fcfg){ var h=hostOf(a.href); if(inList(h,fcfg.whitelist||[])||inList(h,fcfg.blacklist||[]))return; } e.preventDefault();e.stopImmediatePropagation(); if(fcfg&&fcfg.openMode==='tab'){ try{ window.open(a.href,'_blank','noopener'); }catch(err){} }else{ postFrameLink(a.href,titleFromAnchor(a)); } },true); } function findFrameBySource(source){ for(var i=0;i