Skip to content

Commit

Permalink
fix(script): 添加多版本脚本的更新模块适配
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Dec 11, 2023
1 parent e9973e6 commit 4a7aa19
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/interfaces/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { HeaderElement } from '../elements/header';
import { ScriptPanelElement } from '../elements/script.panel';
import { $ } from '../utils/common';
import { StartConfig } from '../utils/start';
import { $store } from '../utils/store';
import { CommonEventEmitter } from './common';
import { Config } from './config';
Expand Down Expand Up @@ -87,6 +88,9 @@ export class Script<
/** 存储已经处理过的 configs 对象,避免重复调用方法 */
private _resolvedConfigs?: C;

/** 启动参数,由外部方法统一赋值 */
startConfig?: StartConfig;

/** 名字 */
name: string;
/** 工程名,如果是独立脚本则为空 */
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface StartConfig {
projects: Project[];
/** 默认面板名称 */
defaultPanelName: string;

[x: string]: any;
}

/**
Expand Down Expand Up @@ -69,6 +71,7 @@ export async function start(startConfig: StartConfig) {

/** 执行脚本 */
scripts.forEach((script) => {
script.startConfig = startConfig;
script.emit('start', startConfig);
script.onstart?.(startConfig);
});
Expand Down
16 changes: 16 additions & 0 deletions packages/scripts/entry.common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* eslint-disable no-undef */
/// <reference path="./global.d.ts" />

const { start, definedProjects, CommonProject } = OCS;

(function () {
'use strict';

// 运行脚本
start({
style: STYLE,
projects: definedProjects(),
defaultPanelName: CommonProject.scripts.guide.namespace,
updatePage: GM_info.scriptHandler === 'Tampermonkey' ? 'https://greasyfork.org/zh-CN/scripts/481438' : 'https://scriptcat.org/zh-CN/script-show-page/1398'
});
})();
3 changes: 2 additions & 1 deletion packages/scripts/entry.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { start, definedProjects, CommonProject } = OCS;
start({
style: GM_getResourceText('STYLE'),
projects: definedProjects(),
defaultPanelName: CommonProject.scripts.guide.namespace
defaultPanelName: CommonProject.scripts.guide.namespace,
updatePage: ''
});
})();
3 changes: 2 additions & 1 deletion packages/scripts/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { start, definedProjects, CommonProject } = OCS;
start({
style: STYLE,
projects: definedProjects(),
defaultPanelName: CommonProject.scripts.guide.namespace
defaultPanelName: CommonProject.scripts.guide.namespace,
updatePage: GM_info.scriptHandler === 'Tampermonkey' ? 'https://greasyfork.org/zh-CN/scripts/457151' : 'https://scriptcat.org/script-show-page/367'
});
})();
23 changes: 11 additions & 12 deletions packages/scripts/src/projects/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,23 +341,17 @@ export const BackgroundProject = Project.create({

const changeLog = el('button', { className: 'base-style-button-secondary' }, '📄查看更新日志');
changeLog.onclick = () => CommonProject.scripts.apps.methods.showChangelog();

const updatePage = this.startConfig?.updatePage || '';
panel.body.replaceChildren(
el('div', { className: 'card' }, [
el('hr'),
el('div', ['最新版本:' + version['last-version'] + ' - ', changeLog]),
el('hr'),
el('div', '当前版本:' + $gm.getInfos()?.script.version),
el('div', '脚本管理器:' + infos?.scriptHandler),
el('div', [
'脚本更新链接:',
el('a', { target: '_blank', href: version.resource[infos.scriptHandler] }, [
version.resource[infos.scriptHandler]
])
])
el('div', '当前版本:' + infos.script.version),
el('div', '脚本管理器:' + infos.scriptHandler),
el('div', ['脚本更新链接:', el('a', { target: '_blank', href: updatePage }, [updatePage || '无'])])
])
);

console.log('versions', {
notToday: this.cfg.notToday,
ignoreVersions: this.cfg.ignoreVersions,
Expand All @@ -380,6 +374,7 @@ export const BackgroundProject = Project.create({
// 版本比较
gt(last, infos.script.version)
) {
const updatePage = this.startConfig?.updatePage || '';
const modal = $modal('confirm', {
maskCloseable: false,
width: 600,
Expand All @@ -399,8 +394,12 @@ export const BackgroundProject = Project.create({
}),
el('button', { className: 'base-style-button', innerText: '前往更新' }, (btn) => {
btn.onclick = () => {
window.open(version.resource[infos.scriptHandler], '_blank');
modal?.remove();
if (updatePage) {
window.open(updatePage, '_blank');
modal?.remove();
} else {
$message('error', { content: '无法前往更新页面,更新链接为空' });
}
};
})
])
Expand Down
2 changes: 1 addition & 1 deletion scripts/build-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ async function createUserJs(cb) {
const commonOpts = createOptions();
commonOpts.metadata.name = commonOpts.metadata.name + ' - 全域名通用版';
commonOpts.metadata.connect = ['*'];
Reflect.deleteProperty(commonOpts.metadata, 'antifeature');
commonOpts.entry = path.join(__dirname, '../packages/scripts/entry.common.js');
commonOpts.dist = path.join(distResolvedPath, 'ocs.common.user.js');

console.log('createUserScript: ', commonOpts.metadata.name, commonOpts.dist);
Expand Down

0 comments on commit 4a7aa19

Please sign in to comment.