single-file-extension-frames.js 3.5 KB

1
  1. !function(){"use strict";const e=globalThis.document;if(e instanceof globalThis.Document){let n=e.createElement("script");n.src="data:,("+t.toString()+")()",(e.documentElement||e).appendChild(n),n.remove(),n=e.createElement("script"),n.textContent="("+t.toString()+")()",(e.documentElement||e).appendChild(n),n.remove()}function t(){"undefined"==typeof globalThis&&(window.globalThis=window);const e=globalThis.document,t=globalThis.console,n=e=>globalThis.dispatchEvent(e),r=globalThis.CustomEvent,a=globalThis.FileReader,o=globalThis.Blob,s=t&&t.warn&&((...e)=>t.warn(...e))||(()=>{}),i="single-file-new-font-face",l={family:"font-family",style:"font-style",weight:"font-weight",stretch:"font-stretch",unicodeRange:"unicode-range",variant:"font-variant",featureSettings:"font-feature-settings"};if(globalThis.FontFace){const t=globalThis.FontFace;let a;globalThis.FontFace=function(){return a||(s("SingleFile is hooking the FontFace constructor, document.fonts.delete and document.fonts.clear to handle dynamically loaded fonts."),a=!0),c(...arguments).then((e=>n(new r(i,{detail:e})))),new t(...arguments)},globalThis.FontFace.toString=function(){return"function FontFace() { [native code] }"};const o=e.fonts.delete;e.fonts.delete=function(t){return c(t.family).then((e=>n(new r("single-file-delete-font",{detail:e})))),o.call(e.fonts,t)},e.fonts.delete.toString=function(){return"function delete() { [native code] }"};const l=e.fonts.clear;e.fonts.clear=function(){return n(new r("single-file-clear-fonts")),l.call(e.fonts)},e.fonts.clear.toString=function(){return"function clear() { [native code] }"}}async function c(e,t,n){const r={};return r["font-family"]=e,r.src=t,n&&Object.keys(n).forEach((e=>{l[e]&&(r[l[e]]=n[e])})),new Promise((e=>{if(r.src instanceof ArrayBuffer){const t=new a;t.readAsDataURL(new o([r.src])),t.addEventListener("load",(()=>{r.src="url("+t.result+")",e(r)}))}else e(r)}))}}const n=globalThis.browser,r=globalThis.document;if(r instanceof globalThis.Document&&n&&n.runtime&&n.runtime.getURL){const e=r.createElement("script");e.src=n.runtime.getURL("/lib/single-file-hooks-frames.js"),e.async=!1,(r.documentElement||r).appendChild(e),e.remove()}const a="single-file-response-fetch";let o=new Map;function s(e){return new Promise(((t,n)=>{var r,o,s,i;r=new CustomEvent("single-file-request-fetch",{detail:e}),window.dispatchEvent(r),o=a,s=function r(o){var s,i,l;o.detail?o.detail.url==e&&(s=a,i=r,l=!1,window.removeEventListener(s,i,l),o.detail.response?t({status:o.detail.status,headers:new Map(o.detail.headers),arrayBuffer:async()=>o.detail.response}):n(o.detail.error)):n()},i=!1,window.addEventListener(o,s,i)}))}browser.runtime.onMessage.addListener((e=>"singlefile.fetchFrame"==e.method&&window.frameId&&window.frameId==e.frameId?async function(e){try{let r=await(t=e.url,n={cache:"force-cache",headers:e.headers},window.fetch(t,n));return 401!=r.status&&403!=r.status&&404!=r.status||(r=await Promise.race([s(e.url),new Promise(((e,t)=>setTimeout((()=>t()),5e3)))])),{status:r.status,headers:[...r.headers],array:Array.from(new Uint8Array(await r.arrayBuffer()))}}catch(e){return{error:e&&e.toString()}}var t,n}(e):"singlefile.fetchResponse"==e.method?async function(e){const t=o.get(e.requestId);t&&(e.error?(t.reject(new Error(e.error)),o.delete(e.requestId)):(e.truncated&&(t.array?t.array=t.array.concat(e.array):(t.array=e.array,o.set(e.requestId,t)),e.finished&&(e.array=t.array)),e.truncated&&!e.finished||(t.resolve({status:e.status,headers:{get:t=>e.headers&&e.headers[t]},arrayBuffer:async()=>new Uint8Array(e.array).buffer}),o.delete(e.requestId))));return{}}(e):void 0))}();