Skip to content

Commit

Permalink
Merge pull request #229 from yuque/refactor/showmessage
Browse files Browse the repository at this point in the history
fix: 修复剪藏语雀文档丢失问题
  • Loading branch information
moshangqi authored Dec 22, 2023
2 parents 9d7bf8d + e356ced commit 8c6ddd9
Show file tree
Hide file tree
Showing 20 changed files with 430 additions and 547 deletions.
38 changes: 27 additions & 11 deletions src/background/actionListener/tab.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
import { OperateTabEnum, IOperateTabData } from '@/isomorphic/background/tab';
import Chrome from '@/background/core/chromeExtension';
import { ContentScriptEvents } from '@/isomorphic/event/contentScript';
import chromeExtension from '@/background/core/chromeExtension';
import { RequestMessage } from './index';

export async function createTabActionListener(
request: RequestMessage<IOperateTabData>,
callback: (params: any) => void,
sender: chrome.runtime.MessageSender,
) {
const { type, url } = request.data;
const { type, url, data } = request.data;
const currentTab = await chromeExtension.tabs.getCurrentTab(sender.tab);
switch (type) {
case OperateTabEnum.screenShot: {
const tabs = await Chrome.tabs.query({ lastFocusedWindow: true });
const res = await Chrome.tabs.captureVisibleTab(tabs[0].windowId as number);
const res = await chromeExtension.tabs.captureVisibleTab(currentTab.windowId as number);
callback(res);
break;
}
case OperateTabEnum.getCurrent: {
const tab = await Chrome.tabs.query({ lastFocusedWindow: true, active: true });
callback(tab?.[0]);
callback(currentTab);
break;
}
case OperateTabEnum.create: {
Chrome.tabs.create({ url });
chromeExtension.tabs.create({ url });
callback(true);
break;
}
case OperateTabEnum.getDocument: {
const res = await Chrome.sendMessageToCurrentTab({
action: ContentScriptEvents.GetDocument,
const result = await chromeExtension.scripting.executeScript({
target: { tabId: currentTab.id as number },
func: () => {
return {
url: window.location.href,
html: document.documentElement.outerHTML,
title: document.title,
};
},
});
callback(res);
callback(result[0].result);
break;
}
case OperateTabEnum.showMessage: {
chromeExtension.scripting.executeScript({
target: { tabId: currentTab.id as number },
args: [{ config: data }],
func: args => {
return window._yuque_ext_app.showMessage(args.config);
},
});
callback(true);
break;
}
default: {
Expand Down
21 changes: 5 additions & 16 deletions src/components/SuperSideBar/impl/ClipAssistant/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ import {
ClipAssistantMessageKey,
ClipAssistantMessageActions,
} from '@/isomorphic/event/clipAssistant';
import {
ContentScriptMessageActions,
ContentScriptMessageKey,
} from '@/isomorphic/event/contentScript';
import Env from '@/isomorphic/env';
import { IClipConfig } from '@/isomorphic/constant/clip';
import LarkIcon from '@/components/LarkIcon';
Expand Down Expand Up @@ -83,18 +79,11 @@ function ClipContent() {
link: { text: string; href: string },
) => {
if (Env.isRunningHostPage) {
window.parent.postMessage(
{
key: ContentScriptMessageKey,
action: ContentScriptMessageActions.ShowMessage,
data: {
text,
type: 'success',
link,
},
},
'*',
);
backgroundBridge.tab.showMessage({
text,
type: 'success',
link,
});
return;
}
message.success(
Expand Down
25 changes: 9 additions & 16 deletions src/core/bridge/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Chrome from '@/core/chrome';
import { MapT, OneArgFunctionT } from '@/common/declare';
import { createStorageBridge } from './stroge';
import { createUserBridge } from './user';
Expand All @@ -20,21 +19,15 @@ export interface IBridgeError {

type IBridgeCallback = (res: any) => void;

export type ICallBridgeImpl = (
bridgeName: string,
params?: IBridgeParams,
callback?: IBridgeCallback,
) => void;

export function callBackgroundBridge(
bridgeName: string,
data?: MapT<any>,
callback?: OneArgFunctionT<any>,
) {
callback = callback || function() {
// ignore
};
Chrome.runtime.sendMessage(
export type ICallBridgeImpl = (bridgeName: string, params?: IBridgeParams, callback?: IBridgeCallback) => void;

export function callBackgroundBridge(bridgeName: string, data?: MapT<any>, callback?: OneArgFunctionT<any>) {
callback =
callback ||
function () {
// ignore
};
chrome.runtime.sendMessage(
{
action: bridgeName,
data,
Expand Down
54 changes: 26 additions & 28 deletions src/core/bridge/background/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,23 @@ export function createTabBridge(impl: ICallBridgeImpl) {
tab: {
async screenShot() {
return new Promise(resolve => {
impl(
BackgroundEvents.OperateTab,
{ type: OperateTabEnum.screenShot },
res => {
resolve(res);
},
);
impl(BackgroundEvents.OperateTab, { type: OperateTabEnum.screenShot }, res => {
resolve(res);
});
});
},
async getCurrent(): Promise<chrome.tabs.Tab | undefined> {
return new Promise(resolve => {
impl(
BackgroundEvents.OperateTab,
{ type: OperateTabEnum.getCurrent },
res => {
resolve(res);
},
);
impl(BackgroundEvents.OperateTab, { type: OperateTabEnum.getCurrent }, res => {
resolve(res);
});
});
},
async create(url: string) {
return new Promise(resolve => {
impl(
BackgroundEvents.OperateTab,
{ type: OperateTabEnum.create, url },
res => {
resolve(res);
},
);
impl(BackgroundEvents.OperateTab, { type: OperateTabEnum.create, url }, res => {
resolve(res);
});
});
},
async getDocument(): Promise<{
Expand All @@ -44,13 +32,23 @@ export function createTabBridge(impl: ICallBridgeImpl) {
title: string;
}> {
return new Promise(resolve => {
impl(
BackgroundEvents.OperateTab,
{ type: OperateTabEnum.getDocument },
res => {
resolve(res);
},
);
impl(BackgroundEvents.OperateTab, { type: OperateTabEnum.getDocument }, res => {
resolve(res);
});
});
},
async showMessage(data: {
type: 'error' | 'success';
text: string;
link?: {
text: string;
href: string;
};
}) {
return new Promise(resolve => {
impl(BackgroundEvents.OperateTab, { type: OperateTabEnum.showMessage, data }, res => {
resolve(res);
});
});
},
},
Expand Down
29 changes: 0 additions & 29 deletions src/core/chrome.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/core/parseDom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class ParseDom {
}

async parseDom(domArray: Element[]) {
const result: Array<string> = [];
let result: Array<string> = [];
for (const dom of domArray) {
if (this.isYuqueContent(dom)) {
try {
const htmlArray = await this.parsePageYuqueContent(dom);
result.push(htmlArray[0]);
result = result.concat(htmlArray);
continue;
} catch (error) {
//
Expand Down
7 changes: 2 additions & 5 deletions src/hooks/useCheckVersion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect } from 'react';
import Chrome from '@/core/chrome';

async function fetchAndParseXML(updateUrl: string): Promise<string | null> {
const response = await fetch(
Expand All @@ -16,9 +15,7 @@ async function fetchAndParseXML(updateUrl: string): Promise<string | null> {
const root = xml.documentElement;

// @ts-ignore
const version = root
.getElementsByTagName('updatecheck')[0]
?.getAttribute?.('version');
const version = root.getElementsByTagName('updatecheck')[0]?.getAttribute?.('version');
return version;
}

Expand All @@ -36,7 +33,7 @@ export function useCheckVersion(): string | null {
};

useEffect(() => {
const manifest = Chrome.runtime.getManifest();
const manifest = chrome.runtime.getManifest();
if (process.env.NODE_ENV !== 'production') {
console.log('update_url: %s', manifest?.update_url);
}
Expand Down
2 changes: 2 additions & 0 deletions src/isomorphic/background/tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ export enum OperateTabEnum {
getCurrent = 'getCurrent',
create = 'create',
getDocument = 'getDocument',
showMessage = 'showMessage',
}

export interface IOperateTabData {
type: OperateTabEnum;
url?: string;
data?: any;
}
16 changes: 0 additions & 16 deletions src/isomorphic/event/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,4 @@ export enum ContentScriptEvents {
LevitateConfigChange = 'contentScript/levitateConfigChange',
ForceUpgradeVersion = 'contentScript/forceUpgradeVersion',
LoginOut = 'contentScript/LoginOut',
GetDocument = 'contentScript/getDocument',
}

export const ContentScriptMessageKey = 'ContentScriptMessageKey';

export enum ContentScriptMessageActions {
ShowMessage = 'showMessage',
}

export interface IShowMessageData {
type: 'error' | 'success';
text: string;
link?: {
text: string;
href: string;
};
}
1 change: 0 additions & 1 deletion src/pages/inject/AreaSelector/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ function App(props: IAppProps) {
const target = e.composedPath()?.[0] as Element;
e.stopImmediatePropagation();
e.preventDefault();
console.log(target, target.closest('.select-confirm'));
if (target.closest('.select-confirm')) {
onSave();
} else if (target?.closest('.select-inner')) {
Expand Down
3 changes: 1 addition & 2 deletions src/pages/inject/WordMark/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import React, {
useImperativeHandle,
useRef,
} from 'react';
import Chrome from '@/core/chrome';
import {
EDITOR_IFRAME_CONTAINER_ID,
EditorMessageType,
Expand Down Expand Up @@ -172,7 +171,7 @@ export default forwardRef<IEditorRef, {}>((props, ref) => {

return (
<iframe
src={Chrome.runtime.getURL('editor.html')}
src={chrome.runtime.getURL('editor.html')}
ref={iframeRef}
id={EDITOR_IFRAME_CONTAINER_ID}
style={{ display: 'none' }}
Expand Down
Loading

0 comments on commit 8c6ddd9

Please sign in to comment.