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

Method searchExistingScript

src/app/repo/scripts.ts:201–292  ·  view source on GitHub ↗
(targetScript: Script, toCheckScriptInfoEqual: boolean = true)

Source from the content-addressed store, hash-verified

199 }
200
201 public async searchExistingScript(targetScript: Script, toCheckScriptInfoEqual: boolean = true): Promise<Script[]> {
202 const removeScriptNameFromURL = (url: string) => {
203 // https://scriptcat.org/scripts/code/{id}/{scriptname}.user.js (单匹配)
204 if (url.startsWith("https://scriptcat.org/scripts/code/") && url.endsWith(".js")) {
205 const idx1 = url.indexOf("/", "https://scriptcat.org/scripts/code/".length);
206 const idx2 = url.indexOf("/", idx1 + 1);
207 if (idx1 > 0 && idx2 < 0) {
208 const idx3 = url.indexOf(".", idx1 + 1);
209 return url.substring(0, idx1 + 1) + "*" + url.substring(idx3);
210 }
211 }
212 // https://update.greasyfork.org/scripts/{id}/{scriptname}.user.js (单匹配)
213 if (url.startsWith("https://update.greasyfork.org/scripts/") && url.endsWith(".js")) {
214 const idx1 = url.indexOf("/", "https://update.greasyfork.org/scripts/".length);
215 const idx2 = url.indexOf("/", idx1 + 1);
216 if (idx1 > 0 && idx2 < 0) {
217 const idx3 = url.indexOf(".", idx1 + 1);
218 return url.substring(0, idx1 + 1) + "*" + url.substring(idx3);
219 }
220 }
221 // https://openuserjs.org/install/{username}/{scriptname}.user.js (复数匹配)
222 if (url.startsWith("https://openuserjs.org/install/") && url.endsWith(".js")) {
223 const idx1 = url.indexOf("/", "https://openuserjs.org/install/".length);
224 const idx2 = url.indexOf("/", idx1 + 1);
225 if (idx1 > 0 && idx2 < 0) {
226 const idx3 = url.indexOf(".", idx1 + 1);
227 return url.substring(0, idx1 + 1) + "*" + url.substring(idx3);
228 }
229 }
230 return url;
231 };
232 const valEqual = (val1: any, val2: any) => {
233 if (val1 && val2 && Array.isArray(val1) && Array.isArray(val2)) {
234 if (val1.length !== val2.length) return false;
235 if (val1.length < 2) {
236 return val1[0] === val2[0];
237 }
238 // 無視次序
239 const s = new Set([...val1, ...val2]);
240 if (s.size !== val1.length) return false;
241 return true;
242 }
243 return val1 === val2;
244 };
245 const isScriptInfoEqual = (script1: Script, script2: Script) => {
246 // @author, @copyright, @license 應該不會改
247 if (!valEqual(script1.metadata.author, script2.metadata.author)) return false;
248 if (!valEqual(script1.metadata.copyright, script2.metadata.copyright)) return false;
249 if (!valEqual(script1.metadata.license, script2.metadata.license)) return false;
250 // @grant, @connect 應該不會改
251 if (!valEqual(script1.metadata.grant, script2.metadata.grant)) return false;
252 if (!valEqual(script1.metadata.connect, script2.metadata.connect)) return false;
253 // @match @include 應該不會改
254 if (!valEqual(script1.metadata.match, script2.metadata.match)) return false;
255 if (!valEqual(script1.metadata.include, script2.metadata.include)) return false;
256 return true;
257 };
258

Callers 2

prepareScriptByCodeFunction · 0.80
scripts.test.tsFile · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected