Skip to content

Commit

Permalink
feat(app): 新增用户脚本,可自定义网络脚本添加到本地脚本,并自动载入本地脚本,极大拓展软件功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed Jun 4, 2022
1 parent 156b2f7 commit 7926b35
Show file tree
Hide file tree
Showing 8 changed files with 636 additions and 39 deletions.
19 changes: 19 additions & 0 deletions packages/app/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { appStore } from './src/store';

export { RemoteMethods } from './src/tasks/remote.register';
export type AppStore = typeof appStore;

export interface UserScripts {
id: number
/** 用户脚本链接 */
url: string;
/** 用户脚本所执行的文件 */
runAtFiles: string[]
/** 是否运行在所有页面 */
runAtAll: boolean;
/**
* 脚本信息
*/
info: any
}
2 changes: 1 addition & 1 deletion packages/web/src/components/file/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
title="启动设置"
>
<div class="form">
<label>自动更新OCS脚本</label>
<label>加载本地脚本</label>
<span class="w-100 text-start">
<a-switch
v-model:checked="data.options.init"
Expand Down
3 changes: 1 addition & 2 deletions packages/web/src/components/terminal/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ export class Process {
: opts.launchOptions.executablePath;
opts.localStorage = opts.localStorage === 'default' ? store.script.localStorage : opts.localStorage;

// opts.localStorage.setting.answererWrappers = JSON.parse(opts.localStorage.setting.answererWrappers);
console.log('opts', opts);
opts.userScripts = store.userScripts.filter(s => s.runAtAll || s.runAtFiles.includes(this.uid));

this.send('launch', opts);
this.launched = true;
Expand Down
97 changes: 94 additions & 3 deletions packages/web/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { reactive, shallowRef } from 'vue';
import { RouteRecordRaw } from 'vue-router';
import setting from '@/pages/setting/index.vue';
import workspace from '@/pages/workspace/index.vue';
import userScripts from '@/pages/user-scripts/index.vue';
import { LaunchScriptsOptions } from '@ocsjs/scripts';
import { path } from '../components/file/File';
import { store } from '../store';
import { GreasyForkUserScript, ScriptCatUserScript, ScriptSearchEngine, CommonUserScript } from '../types/user.script';
import { remote } from '../utils/remote';

const { randomUUID } = require('crypto') as typeof import('crypto');

Expand Down Expand Up @@ -36,6 +39,14 @@ export const config = reactive({
title: '工作区'
}
},
scripts: {
name: 'user-scripts',
path: '/user-scripts',
component: shallowRef(userScripts),
meta: {
title: '用户脚本'
}
},
setting: {
name: 'setting',
path: '/setting',
Expand All @@ -58,18 +69,98 @@ export const config = reactive({
headless: false,
executablePath: 'default'
},

scripts: [
{
name: 'cx-login-phone',
options: {}
options: {} as any
}
],
userDataDir: path.join(store['user-data-path'], 'scriptUserData', uid),
init: true,
localStorage: 'default'
localStorage: 'default',
userScripts: []
} as LaunchScriptsOptions,
null,
4
);
}
},
/** 用户脚本搜索引擎 */
scriptSearchEngines: [
{
name: 'GreasyFork',
homepage: 'https://greasyfork.org',
search: async (keyword: string, page: number, size: number) => {
const data = await remote.methods.call('get', 'https://greasyfork.org/zh-CN/scripts.json?' + new URLSearchParams({
q: keyword,
page: page <= 0 ? '1' : page.toString()
}));

let list = data as GreasyForkUserScript[];

list = list.sort((a, b) => {
return b.daily_installs - a.daily_installs;
});

return list.map(item => {
const ratings = (item.good_ratings / (item.good_ratings + item.bad_ratings)) * 10;

return {
url: item.url,
name: item.name,
description: item.description,
homepage: item.url,
id: item.id,
createTime: new Date(item.created_at).getTime(),
updateTime: new Date(item.code_updated_at).getTime(),
daily_installs: item.daily_installs,
total_installs: item.total_installs,
authors: item.users,
ratings: parseFloat(ratings.toFixed(1)),
code_url: item.code_url,
license: item.license,
version: item.version
} as CommonUserScript;
});
}
}, {
name: 'ScriptCat - 脚本猫',
homepage: 'https://scriptcat.org',
search: async (keyword: string, page: number, size: number) => {
const data = await remote.methods.call('get',
'https://scriptcat.org/api/v1/scripts?' + new URLSearchParams({
count: size.toString(),
page: page <= 0 ? '1' : page.toString(),
keyword
}));

let list = data.list as ScriptCatUserScript[];

list = list.sort((a, b) => {
return b.today_install - a.today_install;
});

return list.map(item => {
return {
id: item.script.script_id,
version: item.script.version,
authors: [{
url: `https://scriptcat.org/users/${item.user_id}`,
name: item.username,
avatar: item.avatar ? `https://scriptcat.org${item.avatar}` : undefined
}],
name: item.name,
description: item.description,
url: `https://scriptcat.org/script-show-page/${item.script.script_id}`,
code_url: `https://scriptcat.org/scripts/code/${item.script.script_id}/${item.name}.user.js`,
ratings: item.score ? (item.score * 2) / 10 : 0,
createTime: item.createtime * 1000,
updateTime: item.updatetime * 1000,
daily_installs: item.today_install,
total_installs: item.total_install
} as CommonUserScript;
});
}
}
] as ScriptSearchEngine[]
});
Loading

0 comments on commit 7926b35

Please sign in to comment.