From abfaf7ba08ade794ace782dbd0560b66c50fb957 Mon Sep 17 00:00:00 2001 From: daief <1437931235@qq.com> Date: Thu, 17 Dec 2020 13:55:05 +0800 Subject: [PATCH] wip: refine & fix --- .prettierrc | 3 ++- src/Setting/Root.vue | 6 +++--- src/data/index.ts | 9 +++------ src/index.ts | 4 ++++ src/proxy/xhr.ts | 40 +++++++++++++++++++--------------------- 5 files changed, 31 insertions(+), 31 deletions(-) diff --git a/.prettierrc b/.prettierrc index 966f968..faa44d0 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,6 @@ { "singleQuote": true, "semi": true, - "arrowParens": "avoid" + "arrowParens": "avoid", + "trailingComma": "all" } diff --git a/src/Setting/Root.vue b/src/Setting/Root.vue index d61356d..dad4dde 100644 --- a/src/Setting/Root.vue +++ b/src/Setting/Root.vue @@ -40,7 +40,7 @@
@@ -124,7 +124,7 @@ export default defineComponent({ () => { Store.updateSetList(state.matchedSetList); }, - { deep: true } + { deep: true }, ); watch( @@ -134,7 +134,7 @@ export default defineComponent({ ? Store.getSetList() : Store.getMatchedSetList(); }, - { immediate: true } + { immediate: true }, ); return { diff --git a/src/data/index.ts b/src/data/index.ts index 5ea19e1..a406be4 100644 --- a/src/data/index.ts +++ b/src/data/index.ts @@ -30,12 +30,12 @@ export const Store = { }, getMatchedSetList(): ISet[] { return Store.getSetList().filter(it => - isMatchUrl(it.domainTest, location.host) + isMatchUrl(it.domainTest, location.host), ); }, findCurrentSet(): ISet { const ruleSet = Store.getSetList().find(it => - new RegExp(it.domainTest, 'ig').test(location.host) + new RegExp(it.domainTest, 'ig').test(location.host), ) || { id: uuid4(), domainTest: location.host, @@ -47,10 +47,7 @@ export const Store = { const store = Store.getStoreObject(); input.forEach(it => { const target = store[it.id]; - if (target) { - return Object.assign(target, it); - } - store[it.id] = it; + store[it.id] = target ? Object.assign(target, it) : it; }); GM_setValue(KEY_SET, store); }, 2000), diff --git a/src/index.ts b/src/index.ts index 5a13978..0ed3d28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,10 @@ import { render } from './Setting'; // import(/* webpackChunkName: 'setting' */ './Setting'); function bootstrap() { + if (vmCtx.top !== vmCtx) { + // 只在顶层页面展示操作 + return; + } let handler: any; let isDrag = false; let isMove = false; diff --git a/src/proxy/xhr.ts b/src/proxy/xhr.ts index cde2715..408e502 100644 --- a/src/proxy/xhr.ts +++ b/src/proxy/xhr.ts @@ -9,22 +9,30 @@ vmCtx.XMLHttpRequest = class extends ( #mockResponse: string; #url: string; #method: string; - #proxyed: boolean = false; constructor() { super(); - ['load', 'error'].forEach(ev => { - this.addEventListener(ev, () => { - if (!this.#proxyed) return; + this.addEventListener('readystatechange', e => { + if (this.readyState !== 4) return; - GM_log(`❗️ [XHR] Response is proxyed:\n`); - console.table({ - method: this.#method, - url: this.#url, - status: this.status, - 'proxyed response': this.#mockResponse, - }); + const ruleSet = Store.findCurrentSet(); + const matchedRule = ruleSet.rules.find(it => + isMatchUrl(it.apiTest, this.#url), + ); + + if (!matchedRule?.response) { + return; + } + + this.#mockResponse = matchedRule.response; + + GM_log(`❗️ [XHR] Response is proxyed:\n`); + console.table({ + method: this.#method, + url: this.#url, + status: this.status, + 'proxyed response': this.#mockResponse, }); }); } @@ -47,16 +55,6 @@ vmCtx.XMLHttpRequest = class extends ( this.#url = `${location.origin}/${url.replace(/^\//, '')}`; } - const ruleSet = Store.findCurrentSet(); - const matchedRule = ruleSet.rules.find(it => - isMatchUrl(it.apiTest, this.#url) - ); - - if (matchedRule?.response) { - this.#mockResponse = matchedRule.response; - this.#proxyed = true; - } - // @ts-ignore return super.open(method, url, ...rest); }