UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 10.1 kB
import*as Common from"../../core/common/common.js";import*as i18n from"../../core/i18n/i18n.js";import*as Platform from"../../core/platform/platform.js";import*as SDK from"../../core/sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";const UIStrings={removeFromIgnoreList:"Remove from ignore list",addScriptToIgnoreList:"Add script to ignore list",addDirectoryToIgnoreList:"Add directory to ignore list",addAllContentScriptsToIgnoreList:"Add all extension scripts to ignore list",addAllThirdPartyScriptsToIgnoreList:"Add all third-party scripts to ignore list"},str_=i18n.i18n.registerUIStrings("models/bindings/IgnoreListManager.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let ignoreListManagerInstance;export class IgnoreListManager{#t;#e;#n;constructor(t){this.#t=t,SDK.TargetManager.TargetManager.instance().addModelListener(SDK.DebuggerModel.DebuggerModel,SDK.DebuggerModel.Events.GlobalObjectCleared,this.clearCacheIfNeeded.bind(this),this),Common.Settings.Settings.instance().moduleSetting("skipStackFramesPattern").addChangeListener(this.patternChanged.bind(this)),Common.Settings.Settings.instance().moduleSetting("skipContentScripts").addChangeListener(this.patternChanged.bind(this)),Common.Settings.Settings.instance().moduleSetting("automaticallyIgnoreListKnownThirdPartyScripts").addChangeListener(this.patternChanged.bind(this)),Common.Settings.Settings.instance().moduleSetting("enableIgnoreListing").addChangeListener(this.patternChanged.bind(this)),this.#e=new Set,this.#n=new Map,SDK.TargetManager.TargetManager.instance().observeModels(SDK.DebuggerModel.DebuggerModel,this)}static instance(t={forceNew:null,debuggerWorkspaceBinding:null}){const{forceNew:e,debuggerWorkspaceBinding:n}=t;if(!ignoreListManagerInstance||e){if(!n)throw new Error(`Unable to create settings: debuggerWorkspaceBinding must be provided: ${(new Error).stack}`);ignoreListManagerInstance=new IgnoreListManager(n)}return ignoreListManagerInstance}static removeInstance(){ignoreListManagerInstance=void 0}addChangeListener(t){this.#e.add(t)}removeChangeListener(t){this.#e.delete(t)}modelAdded(t){this.setIgnoreListPatterns(t);const e=t.sourceMapManager();e.addEventListener(SDK.SourceMapManager.Events.SourceMapAttached,this.sourceMapAttached,this),e.addEventListener(SDK.SourceMapManager.Events.SourceMapDetached,this.sourceMapDetached,this)}modelRemoved(t){this.clearCacheIfNeeded();const e=t.sourceMapManager();e.removeEventListener(SDK.SourceMapManager.Events.SourceMapAttached,this.sourceMapAttached,this),e.removeEventListener(SDK.SourceMapManager.Events.SourceMapDetached,this.sourceMapDetached,this)}clearCacheIfNeeded(){this.#n.size>1024&&this.#n.clear()}getSkipStackFramesPatternSetting(){return Common.Settings.Settings.instance().moduleSetting("skipStackFramesPattern")}setIgnoreListPatterns(t){const e=this.enableIgnoreListing?this.getSkipStackFramesPatternSetting().getAsArray():[],n=[];for(const t of e)!t.disabled&&t.pattern&&n.push(t.pattern);return t.setBlackboxPatterns(n)}getGeneralRulesForUISourceCode(t){return{isContentScript:t.project().type()===Workspace.Workspace.projectTypes.ContentScripts,isKnownThirdParty:t.isKnownThirdParty()}}isUserOrSourceMapIgnoreListedUISourceCode(t){if(t.isUnconditionallyIgnoreListed())return!0;const e=this.uiSourceCodeURL(t);return this.isUserIgnoreListedURL(e,this.getGeneralRulesForUISourceCode(t))}isUserIgnoreListedURL(t,e){if(!this.enableIgnoreListing)return!1;if(e?.isContentScript&&this.skipContentScripts)return!0;if(e?.isKnownThirdParty&&this.automaticallyIgnoreListKnownThirdPartyScripts)return!0;if(!t)return!1;if(this.#n.has(t))return Boolean(this.#n.get(t));const n=this.getSkipStackFramesPatternSetting().asRegExp(),i=n&&n.test(t)||!1;return this.#n.set(t,i),i}sourceMapAttached(t){const e=t.data.client,n=t.data.sourceMap;this.updateScriptRanges(e,n)}sourceMapDetached(t){const e=t.data.client;this.updateScriptRanges(e,void 0)}async updateScriptRanges(t,e){let n=!1;if(IgnoreListManager.instance().isUserIgnoreListedURL(t.sourceURL,{isContentScript:t.isContentScript()})||(n=e?.sourceURLs().some((t=>this.isUserIgnoreListedURL(t,{isKnownThirdParty:e.hasIgnoreListHint(t)})))??!1),!n)return scriptToRange.get(t)&&await t.setBlackboxedRanges([])&&scriptToRange.delete(t),void await this.#t.updateLocations(t);if(!e)return;const i=e.findRanges((t=>this.isUserIgnoreListedURL(t,{isKnownThirdParty:e.hasIgnoreListHint(t)})),{isStartMatching:!0}).flatMap((t=>[t.start,t.end]));!function(t,e){if(t.length!==e.length)return!1;for(let n=0;n<t.length;++n)if(t[n].lineNumber!==e[n].lineNumber||t[n].columnNumber!==e[n].columnNumber)return!1;return!0}(scriptToRange.get(t)||[],i)&&await t.setBlackboxedRanges(i)&&scriptToRange.set(t,i),this.#t.updateLocations(t)}uiSourceCodeURL(t){return t.project().type()===Workspace.Workspace.projectTypes.Debugger?null:t.url()}canIgnoreListUISourceCode(t){const e=this.uiSourceCodeURL(t);return!!e&&Boolean(this.urlToRegExpString(e))}ignoreListUISourceCode(t){const e=this.uiSourceCodeURL(t);e&&this.ignoreListURL(e)}unIgnoreListUISourceCode(t){this.unIgnoreListURL(this.uiSourceCodeURL(t),this.getGeneralRulesForUISourceCode(t))}get enableIgnoreListing(){return Common.Settings.Settings.instance().moduleSetting("enableIgnoreListing").get()}set enableIgnoreListing(t){Common.Settings.Settings.instance().moduleSetting("enableIgnoreListing").set(t)}get skipContentScripts(){return this.enableIgnoreListing&&Common.Settings.Settings.instance().moduleSetting("skipContentScripts").get()}get automaticallyIgnoreListKnownThirdPartyScripts(){return this.enableIgnoreListing&&Common.Settings.Settings.instance().moduleSetting("automaticallyIgnoreListKnownThirdPartyScripts").get()}ignoreListContentScripts(){this.enableIgnoreListing||(this.enableIgnoreListing=!0),Common.Settings.Settings.instance().moduleSetting("skipContentScripts").set(!0)}unIgnoreListContentScripts(){Common.Settings.Settings.instance().moduleSetting("skipContentScripts").set(!1)}ignoreListThirdParty(){this.enableIgnoreListing||(this.enableIgnoreListing=!0),Common.Settings.Settings.instance().moduleSetting("automaticallyIgnoreListKnownThirdPartyScripts").set(!0)}unIgnoreListThirdParty(){Common.Settings.Settings.instance().moduleSetting("automaticallyIgnoreListKnownThirdPartyScripts").set(!1)}ignoreListURL(t){const e=this.urlToRegExpString(t);e&&this.ignoreListRegex(e,t)}ignoreListRegex(t,e){const n=this.getSkipStackFramesPatternSetting().getAsArray();let i=!1;for(let r=0;r<n.length;++r){const s=n[r];(s.pattern===t||e&&s.disabledForUrl===e)&&(s.disabled=!1,s.disabledForUrl=void 0,i=!0)}i||n.push({pattern:t,disabled:!1}),this.enableIgnoreListing||(this.enableIgnoreListing=!0),this.getSkipStackFramesPatternSetting().setAsArray(n)}unIgnoreListURL(t,e){if(e?.isContentScript&&this.unIgnoreListContentScripts(),e?.isKnownThirdParty&&this.unIgnoreListThirdParty(),!t)return;let n=this.getSkipStackFramesPatternSetting().getAsArray();const i=IgnoreListManager.instance().urlToRegExpString(t);if(i){n=n.filter((function(t){return t.pattern!==i}));for(let e=0;e<n.length;++e){const i=n[e];if(!i.disabled)try{new RegExp(i.pattern).test(t)&&(i.disabled=!0,i.disabledForUrl=t)}catch(t){}}this.getSkipStackFramesPatternSetting().setAsArray(n)}}removeIgnoreListPattern(t){let e=this.getSkipStackFramesPatternSetting().getAsArray();e=e.filter((function(e){return e.pattern!==t})),this.getSkipStackFramesPatternSetting().setAsArray(e)}ignoreListHasPattern(t,e){return this.getSkipStackFramesPatternSetting().getAsArray().some((n=>!(e&&n.disabled)&&n.pattern===t))}async patternChanged(){this.#n.clear();const t=[];for(const e of SDK.TargetManager.TargetManager.instance().models(SDK.DebuggerModel.DebuggerModel)){t.push(this.setIgnoreListPatterns(e));const n=e.sourceMapManager();for(const i of e.scripts())t.push(this.updateScriptRanges(i,n.sourceMapForClient(i)))}await Promise.all(t);const e=Array.from(this.#e);for(const t of e)t();this.patternChangeFinishedForTests()}patternChangeFinishedForTests(){}urlToRegExpString(t){const e=new Common.ParsedURL.ParsedURL(t);if(e.isAboutBlank()||e.isDataURL())return"";if(!e.isValid)return"^"+Platform.StringUtilities.escapeForRegExp(t)+"$";let n=e.lastPathComponent;if(n?n="/"+n:e.folderPathComponents&&(n=e.folderPathComponents+"/"),n||(n=e.host),!n)return"";const i=e.scheme;let r="";return i&&"http"!==i&&"https"!==i&&(r="^"+i+"://","chrome-extension"===i&&(r+=e.host+"\\b"),r+=".*"),r+Platform.StringUtilities.escapeForRegExp(n)+(t.endsWith(n)?"$":"\\b")}getIgnoreListURLContextMenuItems(t){if(t.project().type()===Workspace.Workspace.projectTypes.FileSystem)return[];const e=[],n=this.canIgnoreListUISourceCode(t),i=this.isUserOrSourceMapIgnoreListedUISourceCode(t),{isContentScript:r,isKnownThirdParty:s}=this.getGeneralRulesForUISourceCode(t);return i?(n||r||s)&&e.push({text:i18nString(UIStrings.removeFromIgnoreList),callback:this.unIgnoreListUISourceCode.bind(this,t)}):(n&&e.push({text:i18nString(UIStrings.addScriptToIgnoreList),callback:this.ignoreListUISourceCode.bind(this,t)}),e.push(...this.getIgnoreListGeneralContextMenuItems({isContentScript:r,isKnownThirdParty:s}))),e}getIgnoreListGeneralContextMenuItems(t){const e=[];return t?.isContentScript&&e.push({text:i18nString(UIStrings.addAllContentScriptsToIgnoreList),callback:this.ignoreListContentScripts.bind(this)}),t?.isKnownThirdParty&&e.push({text:i18nString(UIStrings.addAllThirdPartyScriptsToIgnoreList),callback:this.ignoreListThirdParty.bind(this)}),e}getIgnoreListFolderContextMenuItems(t,e){const n=[],i="^"+Platform.StringUtilities.escapeForRegExp(t)+"/";return this.ignoreListHasPattern(i,!0)?n.push({text:i18nString(UIStrings.removeFromIgnoreList),callback:this.removeIgnoreListPattern.bind(this,i)}):this.isUserIgnoreListedURL(t,e)?n.push({text:i18nString(UIStrings.removeFromIgnoreList),callback:this.unIgnoreListURL.bind(this,t,e)}):(n.push({text:i18nString(UIStrings.addDirectoryToIgnoreList),callback:this.ignoreListRegex.bind(this,i)}),n.push(...this.getIgnoreListGeneralContextMenuItems(e))),n}}const scriptToRange=new WeakMap;