Skip to content

Commit

Permalink
fix bug & ui 调整
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jun 24, 2021
1 parent 4a68852 commit daff97c
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 142 deletions.
2 changes: 1 addition & 1 deletion build/scriptcat/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "ScriptCat",
"version": "0.4.1",
"version": "0.4.2",
"description": "脚本猫,一个用户脚本的框架,可编写脚本每天帮你自动处理事务.",
"background": {
"page": "background.html"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scriptcat",
"version": "0.4.1",
"version": "0.4.2",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"scripts": {
"test": "jest",
Expand Down
4 changes: 2 additions & 2 deletions public/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
margin: 0;
padding: 0;
border: 0;
width: 500px;
width: 450px;
/* height: 500px; */
min-height: 100px;
min-height: 150px;
max-height: 800px;
/* overflow-y: auto; */
/* overflow: hidden; */
Expand Down
15 changes: 10 additions & 5 deletions scripts/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ var AdmZip = require("adm-zip");
var pjson = require('../package.json');
const { execSync } = require("child_process");

// 处理manifest version
let str = fs.readFileSync("./build/scriptcat/manifest.json").toString();
str = str.replace(/"version": "(.*?)"/, '"version": "' + pjson.version + '"');
fs.writeFileSync("./build/scriptcat/manifest.json", str);

// 处理config.ts version
str = fs.readFileSync("./src/apps/config.ts").toString();
str = str.replace(/ExtVersion = "(.*?)";/, 'ExtVersion = "' + pjson.version + '";');
fs.writeFileSync("./src/apps/config.ts", str);

execSync("npm run build");

execSync("npm run build-no-split");

// 处理manifest version
let jsonStr = fs.readFileSync("./build/scriptcat/manifest.json").toString();
jsonStr = jsonStr.replace(/"version": "(.*?)"/, '"version": "' + pjson.version + '"');
fs.writeFileSync("./build/scriptcat/manifest.json", jsonStr);

// 处理 ts.worker.js 和 editor.worker.js
let list = fs.readdirSync("./build/scriptcat/src");
let monaco = [];
Expand Down
1 change: 1 addition & 0 deletions src/apps/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ExtVersion = "0.4.2";
4 changes: 2 additions & 2 deletions src/apps/script/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export class Background {

public disableScript(script: Script): Promise<void> {
return new Promise(async resolve => {
sandbox.postMessage({ action: 'stop', data: script, isdebug: false }, '*');
sandbox.postMessage({ action: 'disable', data: script}, '*');
function listener(event: MessageEvent) {
if (event.data.action != "stop") {
if (event.data.action != "disable") {
return;
}
resolve();
Expand Down
34 changes: 2 additions & 32 deletions src/apps/script/controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { v5 as uuidv5 } from 'uuid';
import { SCRIPT_STATUS_ENABLE, SCRIPT_STATUS_DISABLE, Script, SCRIPT_RUN_STATUS_COMPLETE, SCRIPT_STATUS_PREPARE, SCRIPT_TYPE_BACKGROUND, SCRIPT_TYPE_CRONTAB, SCRIPT_TYPE_NORMAL } from "@App/model/do/script";
import { SCRIPT_STATUS_ENABLE, SCRIPT_STATUS_DISABLE, Script, SCRIPT_RUN_STATUS_COMPLETE, SCRIPT_STATUS_PREPARE, SCRIPT_TYPE_BACKGROUND, SCRIPT_TYPE_CRONTAB, SCRIPT_TYPE_NORMAL, SCRIPT_ORIGIN_LOCAL } from "@App/model/do/script";
import { ScriptModel } from "@App/model/script";
import { Page } from "@App/pkg/utils";
import { ScriptExec, ScriptStatusChange, ScriptStop, ScriptUninstall, ScriptReinstall, ScriptInstall, RequestInstallInfo, ScriptCheckUpdate, RequestConfirmInfo } from "../msg-center/event";
Expand Down Expand Up @@ -117,36 +117,6 @@ export class ScriptController {
});
}

public loadScriptByUrl(url: string): Promise<ScriptUrlInfo | undefined> {
return new Promise(resolve => {
axios.get(url, {
headers: {
'Cache-Control': 'no-cache'
}
}).then((response): ScriptUrlInfo | undefined => {
if (response.status != 200) {
return undefined;
}
let ok = parseMetadata(response.data);
if (!ok) {
return undefined;
}
let uuid = uuidv5(url, uuidv5.URL);
let ret = {
url: url,
code: response.data,
uuid: uuid
};
App.Cache.set("install:" + uuid, ret);
return ret;
}).then((val) => {
resolve(val);
}).catch((e) => {
resolve(undefined);
});
});
}

public prepareScriptByCode(code: string, url: string): Promise<[Script | undefined, Script | undefined]> {
return new Promise(async resolve => {
let metadata = parseMetadata(code);
Expand Down Expand Up @@ -192,7 +162,7 @@ export class ScriptController {
checktime: 0,
};
let old = await this.scriptModel.findByUUID(script.uuid);
if (!old) {
if (!old && !script.origin.startsWith(SCRIPT_ORIGIN_LOCAL)) {
old = await this.scriptModel.findByName(script.name);
}
if (old) {
Expand Down
2 changes: 1 addition & 1 deletion src/apps/script/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ export class ScriptManager {
onclick: (info, tab) => {
// 通信发送
chrome.tabs.sendMessage(tab.id!, {
"action": "exec", "uuid": script.uuid,
"action": ScriptExec, "uuid": script.uuid,
});
},
documentUrlPatterns: script.metadata['match'],
Expand Down
5 changes: 1 addition & 4 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ function sandboxLoad(event: MessageEvent) {
}
scripts.scriptList({ status: SCRIPT_STATUS_ENABLE }).then((items) => {
items.forEach((value: Script) => {
// 只开启后台脚本
if (value.type != SCRIPT_TYPE_NORMAL) {
scripts.enableScript(value);
}
scripts.enableScript(value);
});
});
window.removeEventListener("message", sandboxLoad);
Expand Down
3 changes: 1 addition & 2 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
browserMsg.send(ScriptValueChange, msg);
})
chrome.runtime.onMessage.addListener((event) => {
console.log(event);
switch (event.action) {
case 'exec':
case ScriptExec:
browserMsg.send(ScriptExec, event.uuid);
break;
}
Expand Down
35 changes: 23 additions & 12 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ async function execScript(
): Promise<boolean> {
return new Promise(async (resolve) => {
//使用SandboxContext接管postRequest
let key: any;
if (type == "debug") {
key = "script:debug:" + script.id;
} else {
key = "script:" + script.id;
}
script.delayruntime = 0;
context.CAT_setRunError("", 0);
script.lastruntime = new Date().getTime();
Expand Down Expand Up @@ -183,25 +177,38 @@ async function exec(script: ScriptCache, isdebug: boolean) {
return top.postMessage({ action: "exec", data: "" }, "*");
}

async function stop(script: Script, isdebug: boolean) {
async function disable(script: Script) {
let context = <SandboxContext>(
await App.Cache.get("script:" + (isdebug ? "debug:" : "") + script.id)
await App.Cache.get("script:" + script.id)
);
if (context) {
context.destruct();
}
if (script.type != SCRIPT_TYPE_CRONTAB) {
return top.postMessage({ action: "stop" }, "*");
return top.postMessage({ action: "disable" }, "*");
}
let list = cronjobMap.get(script.id);
if (!list) {
return top.postMessage({ action: "stop" }, "*");
return top.postMessage({ action: "disable" }, "*");
}
list.forEach((val) => {
val.stop();
});
cronjobMap.delete(script.id);
return top.postMessage({ action: "disable" }, "*");
}

async function stop(script: Script, isdebug: boolean) {
let context = <SandboxContext>(
await App.Cache.get("script:" + (isdebug ? "debug:" : "") + script.id)
);
if (context) {
if (script.type == SCRIPT_TYPE_CRONTAB) {
context.CAT_runComplete();
} else {
context.destruct();
}
}
return top.postMessage({ action: "stop" }, "*");
}

Expand All @@ -221,14 +228,18 @@ window.addEventListener("message", (event) => {
start(event.data.data);
break;
}
case "stop": {
stop(event.data.data, event.data.isdebug);
case "disable": {
disable(event.data.data);
break;
}
case "exec": {
exec(event.data.data, event.data.isdebug);
break;
}
case "stop": {
stop(event.data.data, event.data.isdebug);
break;
}
case ScriptValueChange: {
AppEvent.trigger(ScriptValueChange, event.data.value);
}
Expand Down
35 changes: 0 additions & 35 deletions src/types/script.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/views/components/Tab/Tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { Component, Prop, Provide, Vue, Watch } from "vue-property-decorator";
import CloseButton from "./CloseButton.vue";
import TabPane from "./TabPane";

interface ITabItem {}
interface ITabItem { }

const noop = () => {};
const noop = () => { };

@Component({
components: {
Expand Down
9 changes: 6 additions & 3 deletions src/views/pages/Option/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,19 @@ export default class App extends Vue {
render() {
return (
<VApp>
<v-app-bar color="#1296DB" dense dark>
<v-app-bar-nav-icon></v-app-bar-nav-icon>

<v-toolbar-title>ScriptCat</v-toolbar-title>
<v-spacer></v-spacer>
</v-app-bar>
<div
style={{
height: "100%",
display: "flex",
flexDirection: "column",
}}
>
<Carousel />
<Snackbar />

<Tab ref="tabRef" onTabRemove={this.handleTabRemove}>
{this.allTabs.map((tab) => {
const { title, icon, content, tabKey, ...rest } = tab;
Expand Down
11 changes: 9 additions & 2 deletions src/views/pages/Option/tabs/ScriptList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
>
<div v-for="(log, i) in logs" :key="i">
{{ log.level }} {{ formatTime(log.createtime) }} -
{{ log.message }}
<div v-html="log.message" style="display: inline"></div>
</div>
<div class="d-flex justify-center" style="padding: 4px">
<div>
Expand Down Expand Up @@ -501,8 +501,15 @@ export default class ScriptList extends Vue {
this.showlog = true;
this.logs = await this.scriptController.getScriptLog(
item.id,
new Page(1, 20, "asc")
new Page(1, 20, "desc")
);
this.logs = this.logs.reverse();
setTimeout(() => {
let el = document.querySelector("#log-show");
if (el) {
el.scrollTop = el.scrollHeight;
}
}, 1000);
}
async clearLog(item: Script) {
Expand Down
2 changes: 1 addition & 1 deletion src/views/pages/Popup/ScriptList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
</template>

<template v-else>
<v-list-item @click="scriptController.exec(script.id, false)">
<v-list-item @click="scriptController.stop(script.id, false)">
<v-list-item-icon>
<v-icon>mdi-stop</v-icon>
</v-list-item-icon>
Expand Down
Loading

0 comments on commit daff97c

Please sign in to comment.