Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit 08ee104

Browse files
committed
refactor: replace script element lookup with import.meta.url
The change removes the need to query for script elements in the DOM by using import.meta.url directly, which is more reliable in module contexts. This simplifies the code and improves maintainability.
1 parent d571205 commit 08ee104

File tree

2 files changed

+2
-16
lines changed

2 files changed

+2
-16
lines changed

dist/index.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
import * as tool from './tool.js';
2-
function getScriptElement() {
3-
const scripts = document.querySelectorAll('script');
4-
return scripts[scripts.length - 1];
5-
}
6-
const scriptElement = getScriptElement();
72
function getLocation() {
83
let location = window.location.href;
94
const lio = location.indexOf('?');
@@ -18,7 +13,7 @@ function getLocation() {
1813
}
1914
const location = getLocation();
2015
function getQueryString() {
21-
const uri = tool.parseUrl(scriptElement.src);
16+
const uri = tool.parseUrl(import.meta.url);
2217
const rtn = (uri.query ? tool.queryParse(uri.query) : {});
2318
if (!rtn.cdn) {
2419
rtn.cdn = 'https://cdn.jsdelivr.net';

dist/index.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import * as tool from './tool.js';
22

3-
// --- 预处理 URL ---
4-
5-
function getScriptElement(): HTMLScriptElement {
6-
const scripts = document.querySelectorAll('script');
7-
return scripts[scripts.length - 1];
8-
}
9-
/** --- 加载当前文件的 script 元素 --- */
10-
const scriptElement = getScriptElement();
11-
123
function getLocation(): string {
134
/** --- 浏览器 location 的网址目录,以 / 结尾 --- */
145
let location = window.location.href;
@@ -36,7 +27,7 @@ function getQueryString(): {
3627
'cdn': string;
3728
} {
3829
/** --- uri --- */
39-
const uri = tool.parseUrl(scriptElement.src);
30+
const uri = tool.parseUrl(import.meta.url);
4031
const rtn = (uri.query ? tool.queryParse(uri.query) : {}) as Record<string, any>;
4132
if (!rtn.cdn) {
4233
rtn.cdn = 'https://cdn.jsdelivr.net';

0 commit comments

Comments
 (0)