"use strict";function inPageContext(eventId){let orphaned;window.chrome||(window.chrome={}),chrome.runtime||(chrome.runtime={sendMessage:()=>{}});const EXT_ID="fjnbnpbmkenffdnngjfgmeleoegfcffe",{call,defineProperty}=Object,{dispatchEvent,CustomEvent,Promise,Response,removeEventListener}=window,getDetail=call.bind(Object.getOwnPropertyDescriptor(CustomEvent.prototype,"detail").get),apply=call.bind(Object.apply),mathRandom=Math.random,promiseResolve=async val=>val,startsWith=call.bind("".startsWith),callbacks={__proto__:null},OVR=[[chrome.runtime,"sendMessage",(e,t,args)=>{if(args[0]!==EXT_ID)return apply(e,t,args);const msg=args[1];let res,n=args[args.length-1];return"function"!=typeof n&&(res=new Promise(resolve=>n=resolve)),send("msg",msg,n),res}],[window,"fetch",(e,t,args)=>startsWith(`${args[0]}`,`chrome-extension://${EXT_ID}/`)?promiseResolve(new Response('<!doctype html><html lang="en"></html>')):apply(e,t,args)]];for(let e=0;e<OVR.length;e++){const[obj,name,caller]=OVR[e],orig=obj[name],ovr=new Proxy(orig,{__proto__:null,apply:(e,t,args)=>(orphaned&&restore(obj,name,ovr,e),(orphaned?apply:caller)(e,t,args))});defineProperty(obj,name,{value:ovr}),OVR[e]=[obj,name,ovr,orig]}function restore(obj,name,ovr,orig){obj[name]===ovr&&defineProperty(obj,name,{__proto__:null,value:orig})}function send(cmd,data,e){let t;e&&(callbacks[t=mathRandom()]=e),dispatchEvent(new CustomEvent(eventId+"*",{__proto:null,detail:{id:t,cmd,data}}))}addEventListener(eventId,function onCommand(e){let t=getDetail(e);if("quit"===t.cmd)for(orphaned=!0,removeEventListener(eventId,onCommand,!0),t=0;t<OVR.length;t++)restore(OVR[t]);else callbacks[t.id](t.data),delete callbacks[t.id]},!0),window.isInstalled=!0}(()=>{if(1===window.INJECTED_USO)return;window.INJECTED_USO=1;const pageId=`${performance.now()}${Math.random()}`,STATE_EVENTS=[["uninstalled","styleCanBeInstalledChrome"],["canBeUpdate","styleCanBeUpdatedChrome"],["installed","styleAlreadyInstalledChrome"]],getUsoId=()=>Number(location.pathname.match(/^\/styles\/(\d+)|$/)[1]);let pageLoading,gesture=NaN;function onGesture(e){e.isTrusted&&(gesture=performance.now())}function isTrusted(data){return pageLoading===location.href||performance.now()-gesture<1e3||console.warn("Stylus is ignoring request not initiated by the user:",data)}async function onPageEvent({detail:{id:e,cmd,data}}){if("msg"===cmd){let res=!0;switch(data.type){case"stylishUpdateChrome":case"stylishInstallChrome":isTrusted(data)&&await API.uso.toUsercss(getUsoId(),data.customOptions||{}),res={success:!0},gesture=NaN;break;case"deleteStylishStyle":isTrusted(data)&&(res=await API.uso.deleteStyle(getUsoId())),gesture=NaN;break;case"getStyleInstallStatus":isTrusted(data)&&(res=(await getStyleState()||[])[0]);break;case"GET_OPEN_TABS":case"GET_TOP_SITES":res=[]}sendPageEvent({id:e,data:res})}}async function getStyleState(usoId=getUsoId()){return STATE_EVENTS[usoId?await API.uso.getUpdatability(usoId):-1]}function sendPageEvent(data){"function"==typeof cloneInto&&(data=cloneInto(data,document)),dispatchEvent(new CustomEvent(pageId,{detail:data}))}!function(e,...args){const div=document.createElement("div");div.attachShadow({mode:"closed"}).appendChild(document.createElement("script")).textContent=`(${e})(${JSON.stringify(args).slice(1,-1)})`,document.documentElement.appendChild(div).remove()}(inPageContext,pageId),addEventListener("click",onGesture,!0),addEventListener("keydown",onGesture,!0),addEventListener(pageId+"*",onPageEvent,!0),addEventListener(chrome.runtime.id,function orphanCheck(e){if(chrome.runtime.id)return!0;removeEventListener(e.type,orphanCheck,!0),removeEventListener(pageId+"*",onPageEvent,!0),removeEventListener("click",onGesture,!0),removeEventListener("keydown",onGesture,!0),sendPageEvent({cmd:"quit"})},!0),(pageLoading=!document.head&&location.href)&&(addEventListener("DOMContentLoaded",()=>{postMessage({direction:"from-content-script",message:"StylishInstalled"},"*")},{once:!0}),addEventListener("load",()=>{pageLoading=""},{once:!0}))})();