MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / emptyScript

Function emptyScript

src/pages/options/routes/script/ScriptEditor.tsx:131–197  ·  view source on GitHub ↗
(template: string, hotKeys: any, target?: string)

Source from the content-addressed store, hash-verified

129};
130
131const emptyScript = async (template: string, hotKeys: any, target?: string) => {
132 let code = "";
133 switch (template) {
134 case "background":
135 code = backgroundTpl;
136 code = lazyScriptName(code);
137 break;
138 case "crontab":
139 code = crontabTpl;
140 code = lazyScriptName(code);
141 break;
142 default: {
143 code = normalTpl;
144 const [url, icon] =
145 target === "initial"
146 ? await new Promise<string[]>((resolve) => {
147 chrome.storage.local.get(["activeTabUrl"], (result) => {
148 const lastError = chrome.runtime.lastError;
149 let retUrl = "https://*/*";
150 let retIcon = "";
151 if (lastError) {
152 console.error("chrome.runtime.lastError in chrome.storage.local.get:", lastError);
153 chrome.storage.local.remove(["activeTabUrl"]);
154 } else {
155 chrome.storage.local.remove(["activeTabUrl"]);
156 const pageUrl = result?.activeTabUrl?.url;
157 if (pageUrl) {
158 try {
159 const { protocol, pathname, hostname, search } = new URL(pageUrl);
160 if (protocol && pathname && hostname) {
161 retUrl = `${protocol}//${hostname}${pathname}${search.length > 1 ? search : ""}`;
162 if (protocol === "http:" || protocol === "https:") {
163 retIcon = `https://www.google.com/s2/favicons?sz=64&domain=${hostname}`;
164 }
165 }
166 } catch {
167 // do nothing
168 }
169 }
170 }
171 resolve([retUrl, retIcon]);
172 });
173 })
174 : ["https://*/*", ""];
175 code = lazyScriptName(code);
176 if (icon) {
177 code = code.replace("{{match}}", url);
178 code = code.replace("{{icon}}", icon);
179 } else {
180 code = code.replace("{{match}}", url);
181 code = code.replace(/[\r\n]*[^\r\n]*\{\{icon\}\}[^\r\n]*/, "");
182 }
183 break;
184 }
185 }
186 const prepareScript = await prepareScriptByCode(code, "", uuidv4());
187 const { script } = prepareScript;
188 script.createtime = 0;

Callers 2

ScriptEditorFunction · 0.85
handleDeleteEditorFunction · 0.85

Calls 5

lazyScriptNameFunction · 0.90
prepareScriptByCodeFunction · 0.90
errorMethod · 0.80
getMethod · 0.65
removeMethod · 0.65

Tested by

no test coverage detected