single-file-extension-bootstrap.js 4.5 KB

1
  1. !function(){"use strict";const e=globalThis.singlefileBootstrap,t=33554432;let o,a,n,s,i,r,d,c,u;async function l(t){return i&&"content.autosave"==t.method?(async function(e){a=e.options,"complete"!=document.readyState&&await new Promise((e=>globalThis.addEventListener("load",e)));await v(),a.autoSaveRepeat&&setTimeout((()=>{i&&!d&&(c=!1,a.autoSaveDelay=0,l(e))}),1e3*a.autoSaveRepeatDelay)}(t),{}):"content.maybeInit"==t.method?(m(),{}):"content.init"==t.method?(a=t.options,i=t.autoSaveEnabled,h(),{}):"content.openEditor"==t.method?(b(document)?g(document):h(),{}):"devtools.resourceCommitted"==t.method?(e.pageInfo.updatedResources[t.url]={content:t.content,type:t.type,encoding:t.encoding},{}):void 0}function m(){u==location.href||e.pageInfo.processing||(c=!1,u=location.href,browser.runtime.sendMessage({method:"tabs.init",savedPageDetected:b(document)}).catch((()=>{})),browser.runtime.sendMessage({method:"ui.processInit"}).catch((()=>{})))}async function v(){const t=e.helper;if((!d||r)&&!c)if(d=!0,a.autoSaveDelay&&!r)await new Promise((e=>r=setTimeout(e,1e3*a.autoSaveDelay))),await v();else{const o=window._singleFile_waitForUserScript;let n,s=[];r=null,!a.removeFrames&&globalThis.frames&&globalThis.frames.length&&(s=await e.processors.frameTree.getAsync(a)),n=s&&s.sessionId,a.userScriptEnabled&&o&&await o(t.ON_BEFORE_CAPTURE_EVENT_NAME);const i=t.preProcessDoc(document,globalThis,a);S(i,s),n&&e.processors.frameTree.cleanup(n),t.postProcessDoc(document,i.markedElements,i.invalidElements),a.userScriptEnabled&&o&&await o(t.ON_AFTER_CAPTURE_EVENT_NAME),c=!0,d=!1}}function h(){i&&a&&(a.autoSaveUnload||a.autoSaveLoadOrUnload||a.autoSaveDiscard||a.autoSaveRemove)?o||(globalThis.addEventListener("unload",f),document.addEventListener("visibilitychange",p),o=!0):(globalThis.removeEventListener("unload",f),document.removeEventListener("visibilitychange",p),o=!1)}function p(){"hidden"==document.visibilityState&&a.autoSaveDiscard&&E({autoSaveDiscard:a.autoSaveDiscard})}function f(){!c&&(a.autoSaveUnload||a.autoSaveLoadOrUnload||a.autoSaveRemove)&&E({autoSaveUnload:a.autoSaveUnload,autoSaveRemove:a.autoSaveRemove})}function E({autoSaveUnload:t,autoSaveDiscard:o,autoSaveRemove:n}){const s=e.helper,i=window._singleFile_waitForUserScript;let r=[];!a.removeFrames&&globalThis.frames&&globalThis.frames.length&&(r=e.processors.frameTree.getSync(a)),a.userScriptEnabled&&i&&i(s.ON_BEFORE_CAPTURE_EVENT_NAME);S(s.preProcessDoc(document,globalThis,a),r,{autoSaveUnload:t,autoSaveDiscard:o,autoSaveRemove:n})}function S(t,o,{autoSaveUnload:i,autoSaveDiscard:r,autoSaveRemove:d}={}){const c=e.helper,u=e.pageInfo.updatedResources,l=e.pageInfo.visitDate.getTime();Object.keys(u).forEach((e=>u[e].retrieved=!1)),browser.runtime.sendMessage({method:"autosave.save",tabId:n,tabIndex:s,taskId:a.taskId,content:c.serialize(document),canvases:t.canvases,fonts:t.fonts,stylesheets:t.stylesheets,images:t.images,posters:t.posters,usedFonts:t.usedFonts,shadowRoots:t.shadowRoots,videos:t.videos,imports:t.imports,referrer:t.referrer,frames:o,url:location.href,updatedResources:u,visitDate:l,autoSaveUnload:i,autoSaveDiscard:r,autoSaveRemove:d})}async function g(o){const a=o.querySelector("singlefile-infobar");a&&a.remove(),w(o);const n=e.helper.serialize(o);for(let e=0;e*t<n.length;e++){const o={method:"editor.open",filename:decodeURIComponent(location.href.match(/^.*\/(.*)$/)[1])};o.truncated=n.length>t,o.truncated?(o.finished=(e+1)*t>n.length,o.content=n.substring(e*t,(e+1)*t)):o.content=n,await browser.runtime.sendMessage(o)}}function b(t){const o=e.helper,a=t.documentElement.firstChild;return a.nodeType==Node.COMMENT_NODE&&(a.textContent.includes(o.COMMENT_HEADER)||a.textContent.includes(o.COMMENT_HEADER_LEGACY))}function w(t){t.querySelectorAll("*").forEach((t=>{const o=e.helper.getShadowRoot(t);if(o){w(o);const e=document.createElement("template");e.setAttribute("shadowroot","open"),e.appendChild(o),t.appendChild(e)}}))}e.pageInfo={updatedResources:{},visitDate:new Date},browser.runtime.sendMessage({method:"bootstrap.init"}).then((e=>{a=e.optionsAutoSave;const t=e.options;n=e.tabId,s=e.tabIndex,i=e.autoSaveEnabled,t&&t.autoOpenEditor&&b(document)?"loading"==document.readyState?document.addEventListener("DOMContentLoaded",(()=>g(document))):g(document):h()})),browser.runtime.onMessage.addListener((e=>{if(i&&"content.autosave"==e.method||"content.maybeInit"==e.method||"content.init"==e.method||"content.openEditor"==e.method||"devtools.resourceCommitted"==e.method)return l(e)})),document.addEventListener("DOMContentLoaded",m,!1)}();