Skip to content

Commit

Permalink
feat: 增加全文剪藏功能 & 支持 InjectScript & 侧边栏支持拖拽
Browse files Browse the repository at this point in the history
  • Loading branch information
moshangqi committed Dec 22, 2023
1 parent 9aa25ab commit 98ebe5f
Show file tree
Hide file tree
Showing 66 changed files with 1,790 additions and 1,307 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"@dnd-kit/core": "^6.0.8",
"@dnd-kit/modifiers": "^6.0.1",
"@dnd-kit/sortable": "^7.0.2",
"@mozilla/readability": "^0.5.0",
"antd": "^5.7.3",
"bowser": "^2.11.0",
"classnames": "^2.2.6",
Expand Down
1 change: 1 addition & 0 deletions src/assets/svg/clip-page.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 45 additions & 19 deletions src/background/actionListener/clip.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
import {
OperateClipEnum,
IOperateClipData,
} from '@/isomorphic/background/clip';
import Chrome from '@/background/core/chrome';
import { ContentScriptEvents } from '@/isomorphic/event/contentScript';
import { OperateClipEnum, IOperateClipData } from '@/isomorphic/background/clip';
import chromeExtension from '@/background/core/chromeExtension';
import { RequestMessage } from './index';

export async function createClipActionListener(
request: RequestMessage<IOperateClipData>,
callback: (params: any) => void,

Check warning on line 7 in src/background/actionListener/clip.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

'params' is defined but never used

Check warning on line 7 in src/background/actionListener/clip.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
sender: chrome.runtime.MessageSender,
) {
const { type, isRunningInjectPage } = request.data;
const { type, isRunningHostPage } = request.data;
const currentTab = await chromeExtension.tabs.getCurrentTab(sender.tab);
switch (type) {
case OperateClipEnum.screenOcr: {
const res = await Chrome.sendMessageToCurrentTab({
action: ContentScriptEvents.ScreenOcr,
data: {
isRunningInjectPage,
chromeExtension.scripting.executeScript(
{
target: { tabId: currentTab.id as number },
args: [{ isRunningHostPage }],
func: args => {
return window._yuque_ext_app.clipScreenOcr({
isRunningHostPage: args.isRunningHostPage,
});
},
},
});
callback(res);
res => {
callback(res[0].result);
},
);
break;
}
case OperateClipEnum.selectArea: {
const res = await Chrome.sendMessageToCurrentTab({
action: ContentScriptEvents.SelectArea,
data: {
isRunningInjectPage,
chromeExtension.scripting.executeScript(
{
target: { tabId: currentTab.id as number },
args: [{ isRunningHostPage }],
func: args => {
return window._yuque_ext_app.clipSelectArea({
isRunningHostPage: args.isRunningHostPage,
});
},
},
res => {
callback(res[0].result);
},
);
break;
}
case OperateClipEnum.clipPage: {
chromeExtension.scripting.executeScript(
{
target: { tabId: currentTab?.id as number },
func: () => {
return window._yuque_ext_app.parsePage();
},
},
res => {
callback(res[0]?.result);
},
});
callback(res);
);
break;
}
default: {
Expand Down
6 changes: 2 additions & 4 deletions src/background/actionListener/configManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { levitateConfigManager } from '@/background/core/configManager/levitate';
import {
IOperateConfigManagerData,
OperateConfigManagerEnum,
} from '@/isomorphic/background/configManager';
import { IOperateConfigManagerData, OperateConfigManagerEnum } from '@/isomorphic/background/configManager';
import { wordMarkConfigManager } from '../core/configManager/wordMark';
import { clipConfigManager } from '../core/configManager/clip';
import { RequestMessage } from './index';
Expand All @@ -16,6 +13,7 @@ const managerMap = {
export async function createManagerConfigActionListener(
request: RequestMessage<IOperateConfigManagerData>,
callback: (params: any) => void,

Check warning on line 15 in src/background/actionListener/configManager.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

'params' is defined but never used

Check warning on line 15 in src/background/actionListener/configManager.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
sender: chrome.runtime.MessageSender,

Check warning on line 16 in src/background/actionListener/configManager.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

'sender' is defined but never used

Check warning on line 16 in src/background/actionListener/configManager.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

'sender' is defined but never used
) {
const { type, value, key, managerType, option = {} } = request.data;
const manage = managerMap[managerType];
Expand Down
24 changes: 9 additions & 15 deletions src/background/actionListener/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Chrome from '@/background/core/chrome';
import Chrome from '@/background/core/chromeExtension';
import { BackgroundEvents } from '@/isomorphic/background';
import { createStorageActionListener } from './storage';
import { createUserActionListener } from './user';
Expand All @@ -8,8 +8,6 @@ import { createSidePanelActionListener } from './sidePanel';
import { createRequestActionListener } from './request';
import { createManagerConfigActionListener } from './configManager';

type MessageSender = chrome.runtime.MessageSender;

type SendResponse = (response: any) => void;

Check warning on line 11 in src/background/actionListener/index.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

'response' is defined but never used

Check warning on line 11 in src/background/actionListener/index.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type

export interface RequestMessage<T> {
Expand All @@ -19,38 +17,34 @@ export interface RequestMessage<T> {

export const initBackGroundActionListener = () => {
Chrome.runtime.onMessage.addListener(
(
request: RequestMessage<any>,
_sender: MessageSender,
sendResponse: SendResponse,
) => {
(request: RequestMessage<any>, _sender: chrome.runtime.MessageSender, sendResponse: SendResponse) => {

Check warning on line 20 in src/background/actionListener/index.ts

View workflow job for this annotation

GitHub Actions / Runner (ubuntu-latest, 16)

Unexpected any. Specify a different type
switch (request.action) {
case BackgroundEvents.OperateUser: {
createUserActionListener(request, sendResponse);
createUserActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateStorage: {
createStorageActionListener(request, sendResponse);
createStorageActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateClip: {
createClipActionListener(request, sendResponse);
createClipActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateTab: {
createTabActionListener(request, sendResponse);
createTabActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateSidePanel: {
createSidePanelActionListener(request, sendResponse);
createSidePanelActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateRequest: {
createRequestActionListener(request, sendResponse);
createRequestActionListener(request, sendResponse, _sender);
break;
}
case BackgroundEvents.OperateManagerConfig: {
createManagerConfigActionListener(request, sendResponse);
createManagerConfigActionListener(request, sendResponse, _sender);
break;
}
default: {
Expand Down
1 change: 1 addition & 0 deletions src/background/actionListener/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { RequestMessage } from './index';
export async function createRequestActionListener(
request: RequestMessage<IOperateRequestData>,
callback: (params: any) => void,
sender: chrome.runtime.MessageSender,
) {
const { url, config, options = {} } = request.data;

Expand Down
42 changes: 24 additions & 18 deletions src/background/actionListener/sidePanel.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import {
OperateSidePanelEnum,
IOperateSidePanelData,
} from '@/isomorphic/background/sidePanel';
import Chrome from '@/background/core/chrome';
import { ContentScriptEvents } from '@/isomorphic/event/contentScript';
import { OperateSidePanelEnum, IOperateSidePanelData } from '@/isomorphic/background/sidePanel';
import chromeExtension from '../core/chromeExtension';
import { RequestMessage } from './index';

export async function createSidePanelActionListener(
request: RequestMessage<IOperateSidePanelData>,
callback: (params: any) => void,
sender: chrome.runtime.MessageSender,
) {
const { type } = request.data;
const currentTab = await chromeExtension.tabs.getCurrentTab(sender.tab);
switch (type) {
case OperateSidePanelEnum.close: {
const res = await Chrome.sendMessageToCurrentTab({
action: ContentScriptEvents.ToggleSidePanel,
data: {
forceVisible: false,
chromeExtension.scripting.executeScript(
{
target: { tabId: currentTab?.id as number },
func: () => {
return window._yuque_ext_app.toggleSidePanel(false);
},
},
});
callback(res);
res => {
callback(res[0]?.result);
},
);
break;
}
case OperateSidePanelEnum.open: {
const res = await Chrome.sendMessageToCurrentTab({
action: ContentScriptEvents.ToggleSidePanel,
data: {
forceVisible: true,
chromeExtension.scripting.executeScript(
{
target: { tabId: currentTab?.id as number },
func: () => {
return window._yuque_ext_app.toggleSidePanel(true);
},
},
res => {
callback(res[0]?.result);
},
});
callback(res);
);
break;
}
default: {
Expand Down
6 changes: 2 additions & 4 deletions src/background/actionListener/storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {
IOperateStorageData,
OperateStorageEnum,
} from '@/isomorphic/background/storage';
import { IOperateStorageData, OperateStorageEnum } from '@/isomorphic/background/storage';
import { storage } from '@/isomorphic/storage';
import { RequestMessage } from './index';

export async function createStorageActionListener(
request: RequestMessage<IOperateStorageData>,
callback: (params: any) => void,
sender: chrome.runtime.MessageSender,
) {
const { type, key, data } = request.data;
switch (type) {
Expand Down
8 changes: 3 additions & 5 deletions src/background/actionListener/tab.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {
OperateTabEnum,
IOperateTabData,
} from '@/isomorphic/background/tab';
import Chrome from '@/background/core/chrome';
import { OperateTabEnum, IOperateTabData } from '@/isomorphic/background/tab';
import Chrome from '@/background/core/chromeExtension';
import { ContentScriptEvents } from '@/isomorphic/event/contentScript';
import { RequestMessage } from './index';

export async function createTabActionListener(
request: RequestMessage<IOperateTabData>,
callback: (params: any) => void,
sender: chrome.runtime.MessageSender,
) {
const { type, url } = request.data;
switch (type) {
Expand Down
15 changes: 4 additions & 11 deletions src/background/actionListener/user.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { pick } from 'lodash';
import Chrome from '@/background/core/chrome';
import {
IOperateUserData,
OperateUserEnum,
} from '@/isomorphic/background/user';
import Chrome from '@/background/core/chromeExtension';
import { IOperateUserData, OperateUserEnum } from '@/isomorphic/background/user';
import { IUser } from '@/isomorphic/interface';
import { storage } from '@/isomorphic/storage';
import requestFn from '@/background/core/request';
Expand Down Expand Up @@ -60,6 +57,7 @@ const removeWindow = (windowId: number) => {
export async function createUserActionListener(
request: RequestMessage<IOperateUserData>,
callback: (params: any) => void,
sender: chrome.runtime.MessageSender,
) {
const { type } = request.data;
switch (type) {
Expand All @@ -76,12 +74,7 @@ export async function createUserActionListener(
});
if (status === 200) {
const accountInfo = (data as any).data as IUser;
const value = pick(accountInfo, [
'id',
'login',
'name',
'avatar_url',
]);
const value = pick(accountInfo, ['id', 'login', 'name', 'avatar_url']);
const newValue = {
...value,
login_at: Date.now(),
Expand Down
48 changes: 22 additions & 26 deletions src/background/browser-action.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import Chrome from '@/background/core/chrome';
import { ContentScriptEvents } from '@/isomorphic/event/contentScript';

function remindToRefreshPage(tabId: number) {
const msg = __i18n('你需要重新加载该页面才能剪藏。请重新加载页面后再试一次');
Chrome.scripting.executeScript({
target: { tabId },
args: [{ msg }],
func: (args: { msg: string }) => {
window.alert(args.msg); // eslint-disable-line
},
});
}
import chromeExtension from './core/chromeExtension';

export function listenBrowserActionEvent() {
Chrome.action.onClicked.addListener(tab => {
Chrome.tabs.sendMessage(
tab.id as number,
chrome.action.onClicked.addListener(async tab => {
const currentTab = await chromeExtension.tabs.getCurrentTab(tab);
chromeExtension.scripting.executeScript(
{
action: ContentScriptEvents.ToggleSidePanel,
target: { tabId: currentTab?.id as number },
func: () => {
try {
return window._yuque_ext_app.toggleSidePanel();
} catch (e) {
return { error: e };
}
},
},
() => {
/**
* 插件更新后会断链接,需要提醒用户手动刷新下页面
*/
if (
Chrome.runtime.lastError?.message ===
'Could not establish connection. Receiving end does not exist.'
) {
remindToRefreshPage(tab.id as number);
res => {
if (res[0]?.result?.error) {
const msg = __i18n('你需要重新加载该页面才能剪藏。请重新加载页面后再试一次');
chromeExtension.scripting.executeScript({
target: { tabId: currentTab?.id as number },
args: [{ msg }],
func: (args: { msg: string }) => {
window.alert(args.msg); // eslint-disable-line
},
});
}
},
);
Expand Down
Loading

0 comments on commit 98ebe5f

Please sign in to comment.