Skip to content

Commit

Permalink
fix(script): 修复部分脚本管理器无法读取 @connect 头部元数据的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Dec 13, 2023
1 parent bec8b93 commit 69cec2f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
18 changes: 18 additions & 0 deletions packages/core/src/utils/tampermonkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,23 @@ export const $gm = {
silent: silent,
timeout: duration * 1000
});
},
getMetadataFromScriptHead(key: string) {
const metadataString = this.getInfos()?.scriptMetaStr;

if (!metadataString) {
return [];
} else {
const metadata = metadataString.match(/\/\/\s+==UserScript==([\s\S]+)\/\/\s+==\/UserScript==/)?.[1] || '';
const metadataList = (metadata.match(/\/\/\s+@(.+?)\s+(.*?)(?:\n|$)/g) || []).map((line) => {
const words = line.match(/[\S]+/g) || [];
return {
key: (words[1] || '').replace('@', ''),
value: words.slice(2).join(' ')
};
});

return metadataList.filter((l) => l.key === key).map((l) => l.value);
}
}
};
14 changes: 9 additions & 5 deletions packages/scripts/src/projects/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ export const CommonProject = Project.create({
el('button', '确定', (btn) => {
btn.className = 'modal-confirm-button';
btn.onclick = async () => {
// @ts-ignore
const connects: string[] = $gm.getInfos()?.script.connects;
const connects: string[] = $gm.getMetadataFromScriptHead('connect');

const value = textarea.value;

Expand Down Expand Up @@ -286,9 +285,16 @@ export const CommonProject = Project.create({
])
});

// 格式化文本
textarea.value = JSON.stringify(awsResult, null, 4);

// 检测是否有域名白名单
const notAllowed: string[] = [];
console.log(connects);

// 如果是通用版本,则不检测
if (connects.includes('*')) {
return;
}

for (const aw of awsResult) {
if (
Expand Down Expand Up @@ -321,8 +327,6 @@ export const CommonProject = Project.create({
])
});
}

textarea.value = JSON.stringify(awsResult, null, 4);
} else {
$modal('alert', { content: '题库配置不能为空,请重新配置。' });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/scripts/src/projects/icourse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const ICourseProject = Project.create({
dispatcher: new Script({
name: '调度器',
hideInPanel: true,
url: [['所有页面', 'icourse163.org']],
url: [['所有页面', 'icourse163.org']],
oncomplete() {
setInterval(() => {
const hash = new URL(window.location.href).hash;
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function createUserJs(cb) {
commonOpts.metadata.name = commonOpts.metadata.name + ' - 全域名通用版';
const connect = Array.isArray(commonOpts.metadata.connect) ? commonOpts.metadata.connect : [];
connect.push('*');
commonOpts.metadata.connect = connect
commonOpts.metadata.connect = connect;
commonOpts.entry = path.join(__dirname, '../packages/scripts/entry.common.js');
commonOpts.dist = path.join(distResolvedPath, 'ocs.common.user.js');

Expand Down

0 comments on commit 69cec2f

Please sign in to comment.