-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
<!-- 首先,感谢你的贡献!😄 在维护者审核通过后会合并。 请确保填写以下 pull request 的信息,谢谢!~ --> ### 🤔 这个变动的性质是? - [ ] 新特性提交 - [x] 日常 bug 修复 - [ ] 性能优化 - [ ] 功能增强 - [ ] 重构 - [ ] 代码风格优化 - [ ] 测试用例 - [ ] 其他改动(是关于什么的改动?) ### 🔗 相关 Issue <!-- 1. 描述相关需求的来源,如相关的 issue 讨论链接。 2. 例如 close #xxxx、 fix #xxxx --> 无法通过右键菜单创建剪藏 ### 💡 需求背景和解决方案 无法通过右键菜单创建剪藏 <!-- 1. 要解决的具体问题。 2. 列出最终的 API 实现和用法。 3. 涉及UI/交互变动需要有截图或 GIF。 --> ### 📝 更新日志 <!-- 从用户角度描述具体变化,以及可能的 breaking change 和其他风险。 -->
- Loading branch information
Showing
5 changed files
with
107 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { GLOBAL_EVENTS } from '@/events'; | ||
import Chrome from './chrome'; | ||
|
||
type MessageSender = chrome.runtime.MessageSender; | ||
|
||
type SendResponse = (response: boolean) => void; | ||
|
||
export interface RequestMessage { | ||
action: keyof typeof GLOBAL_EVENTS; | ||
HTMLs?: string[]; | ||
} | ||
|
||
export const ActionListener: { | ||
currentType: string | null; | ||
HTMLs: string[]; | ||
listener: ((request: RequestMessage) => void)[]; | ||
addListener: (callback: (request: RequestMessage) => void) => () => void; | ||
getSelectHTMLs: () => string[]; | ||
} = { | ||
currentType: null, | ||
listener: [], | ||
HTMLs: [], | ||
getSelectHTMLs() { | ||
const HTMLs = this.HTMLs; | ||
this.HTMLs = []; | ||
return HTMLs; | ||
}, | ||
addListener(callback: (request: RequestMessage) => void) { | ||
this.listener.push(callback); | ||
return () => { | ||
this.listener = this.listener.filter(item => item !== callback); | ||
}; | ||
}, | ||
}; | ||
|
||
const onReceiveMessage = async ( | ||
request: RequestMessage, | ||
_sender: MessageSender, | ||
sendResponse: SendResponse, | ||
) => { | ||
ActionListener.listener.forEach(listener => listener(request)); | ||
if (request.action === GLOBAL_EVENTS.GET_SELECTED_TEXT) { | ||
ActionListener.currentType = 'selection'; | ||
ActionListener.HTMLs = request.HTMLs || []; | ||
} | ||
sendResponse(true); | ||
}; | ||
|
||
Chrome.runtime.onMessage.addListener(onReceiveMessage); | ||
window.addEventListener('beforeunload', () => { | ||
Chrome.runtime.onMessage.removeListener(onReceiveMessage); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters