Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uninstall #129

Merged
merged 4 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@mui/material": "5.15.5",
"@mui/system": "5.15.6",
"@reduxjs/toolkit": "1.8.4",
"@types/chrome": "0.0.266",
"@types/mockjs": "1.0.10",
"@types/react-copy-to-clipboard": "5.0.7",
"@types/redux-thunk": "2.1.0",
Expand Down
16 changes: 13 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 36 additions & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,54 @@
import browser from 'webextension-polyfill';

import { FIRST_INSTALL_TASK } from '../constants';
import store from '../app/store';
import { isDev } from '../shared/utils';

store.subscribe(() => {
console.log('state', store.getState());
});

let firstInstall = false;

// show welcome page on new install
browser.runtime.onInstalled.addListener(async (details) => {
if (details.reason === 'install') {
console.log('extension first install');
firstInstall = true;

//show the welcome page
const url = browser.runtime.getURL(isDev ? 'src/welcome/welcome.html' : 'welcome.html'); // TODO: better approach
await browser.tabs.create({ url });
} else if (details.reason === 'update') {
// 插件更新时执行脚本
console.log('extension update');
}
});

// background.js
chrome.runtime.onMessage.addListener(function (request: any, sender: any, sendResponse: any) {
if (request.type === 'content_script_loaded') {
console.log('Content script loaded ');
if (firstInstall == true) {
firstInstall = false;
chrome.tabs // 查询所有活动的标签页
.query({ active: true, currentWindow: true }, (tabs) => {
// 获取当前活动标签页
const activeTab = tabs[0];

if (activeTab) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果我登录后关闭 twitter,然后再卸载插件呢

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没问题 因为这个代码是插件加载成功之后,给background发消息,background做的回应。所以这个场景下twitter页面一定存在

// 发送消息到 Content 脚本, 处理首次安装的事物
chrome.tabs.sendMessage(activeTab.id ?? 0, FIRST_INSTALL_TASK, (response) => {
console.log('Received response from content script:', response);
});
}
});
}
}
});

// 监听插件即将停止或卸载的事件
chrome.runtime.onSuspend.addListener(() => {
console.log('Extension is being suspended or uninstalled.');
});

export {};
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const OAUTH2 = 'oauth2';
export const XFANS_DONE = 'Done';
export const XFANS_VERIFY = 'Verify';
export const XFANS_GO = 'GO';

export const FIRST_INSTALL_TASK = 'FIRST_INSTALL_TASK';
// table
export const ROWS_PER_PAGE = 5;

Expand Down
29 changes: 28 additions & 1 deletion src/content/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import React, { ReactElement } from 'react';
import { createRoot } from 'react-dom/client';
import { Provider } from 'react-redux';
import { Store } from '@eduardoac-skimlinks/webext-redux';

import { XFANS_TOKEN } from '../constants';
import { proxyStore as store } from '../app/proxyStore';
import useGlobalStore from '../store/useGlobalStore';
import { addTwitterComponent, addUserPagePriceComponent } from './addToTwitterHome';
import { FIRST_INSTALL_TASK } from '../constants';
import Content from './Content';

import '../tailwind.css';
Expand Down Expand Up @@ -58,3 +59,29 @@ async function withProxyStore(children: ReactElement, proxyStore: Store): Promis
return <Provider store={proxyStore}>{children}</Provider>;
});
}

// 监听来自 Background 脚本的消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如何确保这里收到的就是来自 background 的消息

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯 目前场景没有其他业务发送消息。
我加了一个消息下判断做了筛选

if (message === FIRST_INSTALL_TASK) {
console.log('monitoring Background script messages');
useGlobalStore.getState().logout();
/*
说明:
1. 如果在用户删除插件的时候,刚刚登陆过了插件,插件上会残留xfans_token的值,在我们的登录流程中,我们正是通过这个值,写入到globalStore的token值中。
2. 首次登录的时候,需要移除这个上一次登录产生的xfans_token的值,不然的话会触发自动登录逻辑。
*/
const url = new URL(window.location.href); // 获取当前URL
url.searchParams.delete(XFANS_TOKEN); // 删除指定的查询参数
// 使用 history.replaceState 更新 URL
window.history.replaceState(null, '', url.toString());
}

// 如果消息不需要异步处理,直接返回 true
return true; // 如果异步处理,需要调用 sendResponse
});

// content_script.js
chrome.runtime.sendMessage({ type: 'content_script_loaded' }, function (response) {
// 其他逻辑...
console.log('content script loaded');
});
1 change: 0 additions & 1 deletion src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const manifest: ManifestV3Export = {
{
matches: ['https://twitter.com/*'],
js: ['src/content/index.tsx'],
run_at: 'document_end',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个你不是要解决 interval handle resize 的问题的么,这里删了,那边又行了?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个document_end没有解决问题,实际还是过早,还是在load的事件里面处理

},
],
host_permissions: ['https://twitter.com/*', 'https://x.com/*'],
Expand Down
Loading