single-file-extension-frames.js 3.0 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",c={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),l(...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 l(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 c=e.fonts.clear;e.fonts.clear=function(){return n(new r("single-file-clear-fonts")),c.call(e.fonts)},e.fonts.clear.toString=function(){return"function clear() { [native code] }"}}async function l(e,t,n){const r={};return r["font-family"]=e,r.src=t,n&&Object.keys(n).forEach((e=>{c[e]&&(r[c[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()}let a=new Map;browser.runtime.onMessage.addListener((e=>"singlefile.fetchFrame"==e.method&&window.frameId&&window.frameId==e.frameId?async function(e){try{const r=await(t=e.url,n={cache:"force-cache",headers:e.headers},window.fetch(t,n));return{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=a.get(e.requestId);t&&(e.error?(t.reject(new Error(e.error)),a.delete(e.requestId)):(e.truncated&&(t.array?t.array=t.array.concat(e.array):(t.array=e.array,a.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}),a.delete(e.requestId))));return{}}(e):void 0))}();