(()=>{"use strict";const e={ruffleEnable:!0,ignoreOptout:!1,warnOnUnsupportedContent:!0,logLevel:"error",showSwfDownload:!1};let t,s,n,r,o,a,i,c,u,l,d;function g(e){return new Promise(((t,s)=>{e((e=>{const n=chrome.runtime.lastError;n?s(n):t(e)}))}))}function h(e){return{get:t=>g((s=>e.get(t||null,s))),remove:t=>g((s=>e.remove(t,s))),set:t=>g((s=>e.set(t,s)))}}if("undefined"!=typeof chrome)t=chrome.i18n,s={local:h(chrome.storage.local),sync:h(chrome.storage.sync),onChanged:{addListener:e=>chrome.storage.onChanged.addListener(e)}},n={reload:e=>g((t=>chrome.tabs.reload(e,void 0,t))),query:e=>g((t=>chrome.tabs.query(e,t))),sendMessage:(e,t,s)=>g((n=>chrome.tabs.sendMessage(e,t,s||{},n)))},r=chrome.runtime,o=()=>g((e=>chrome.tabs.create({url:"/options.html"},e)));else{if("undefined"==typeof browser)throw new Error("Extension API not found.");t=browser.i18n,s=browser.storage,n=browser.tabs,r=browser.runtime,o=()=>browser.runtime.openOptionsPage()}class f{constructor(e,t){this.checkbox=e,this.label=t}get input(){return this.checkbox}get value(){return this.checkbox.checked}set value(e){this.checkbox.checked=e}}class b{constructor(e,t){this.select=e,this.label=t}get input(){return this.select}get value(){const e=this.select.selectedIndex;return this.select.options[e].value}set value(e){const t=Array.from(this.select.options).findIndex((t=>t.value===e));this.select.selectedIndex=t}}function m(e){const[t]=e.getElementsByTagName("label"),[s]=e.getElementsByTagName("input");if(s&&"checkbox"===s.type)return new f(s,t);const[n]=e.getElementsByTagName("select");if(n)return new b(n,t);throw new Error("Unknown option element")}const _={status_init:"gray",status_no_tabs:"red",status_tabs_error:"red",status_message_init:"gray",status_result_protected:"gray",status_result_error:"red",status_result_running:"green",status_result_optout:"gray",status_result_disabled:"gray"};function p(){if(!c)return;const e=!function(e,t){for(const[s,n]of Object.entries(e))if(t[s]!==n)return!1;for(const[s,n]of Object.entries(t))if(e[s]!==n)return!1;return!0}(i,c);d.disabled=!e}function y(){!async function(e){let t,s;e("status_init");try{if(t=await n.query({currentWindow:!0,active:!0}),t.length<1)return void e("status_no_tabs");if(t.length>1)throw new Error(`Got ${t.length} tabs in response to active tab query.`)}catch(t){return void e("status_tabs_error")}a=t[0],e("status_message_init");try{s=await n.sendMessage(a.id,{type:"ping"})}catch(t){return e("status_result_protected"),void(d.disabled=!0)}s?(c=s.tabOptions,s.loaded?e("status_result_running"):c.ruffleEnable?e("status_result_optout"):e("status_result_disabled"),p()):e("status_result_error")}((e=>{u.style.setProperty("--color",_[e]),l.textContent=t.getMessage(e)}))}window.addEventListener("DOMContentLoaded",(()=>{(async function(n){const r=function(){const e=new Map;for(const t of document.getElementsByClassName("option")){const s=m(t),n=s.input.id.replace(/[^a-z\d](.)/gi,((e,t)=>t.toUpperCase()));e.set(n,s)}return e}(),o=await async function(){const t=await s.sync.get();return Object.assign(Object.assign({},e),t)}();for(const[e,n]of r.entries()){n.value=o[e],n.label.classList.add("notransition"),n.label.offsetHeight,n.label.classList.remove("notransition");const r=t.getMessage(`settings_${n.input.id}`);r&&(n.label.textContent=r),n.input.addEventListener("change",(()=>{const t=n.value;o[e]=t,s.sync.set({[e]:t})}))}s.onChanged.addListener(((e,t)=>{if("sync"===t){for(const[t,s]of Object.entries(e)){const e=r.get(t);e&&(e.value=s.newValue,o[t]=s.newValue)}n&&n(o)}})),n&&n(o)})((e=>{i=e,p()})),u=document.getElementById("status-indicator"),l=document.getElementById("status-text");const r=document.getElementById("options-button");r.textContent=t.getMessage("open_settings_page"),r.addEventListener("click",(()=>o())),d=document.getElementById("reload-button"),d.textContent=t.getMessage("action_reload"),d.addEventListener("click",(async()=>{await n.reload(a.id),setTimeout((()=>{y()}),1e3)})),y()}))})();