Comments (2)
消息传递
chrome extension onMessage, sendMessage 不支持直接异步,使用时需要注意 ref
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting === "hello")
setTimeout(() => sendResponse({farewell: "goodbye"}), 1000)
}
// return true
);
直接使用,这里 sendMessage 的 response 是拿不到响应的,原因是 onMessage 的响应处理是同步的,这里在 setTimeout 的处理时,通信已关闭了,解决方法是:return true
表示惰性关闭,在sendReponse 后关闭。
这里也无法直接将 linsten 函数设置为异步,里面仅可使用 Promise then 的方式
from cratehub.
工具使用 webext-bridge
webext-bridge 内部维护了通信序列,管理各端口(background, options ...) 的通信,也做了兼容处理
async () => {
const res = await sendMessage(
"get-preferences",
{ sync: false, name: 'hehehai' },
"background"
)
console.log(res)
}
onMessage<{ name: string; sync: boolean }, "get-preferences">(
"get-preferences",
async ({ data }) => {
console.log(data)
const res = await fetch(`https://api.github.com/users/${data.name}`)
return await res.json()
}
)
这里要设置 sync: false
from cratehub.
Related Issues (19)
- bug: deps 解析不完整 HOT 2
- enhancement: 展示更多依赖数据
- bug: 较高的cpu占用 HOT 1
- bug: 页面前进后退,插件模块会消失
- docs:cratehub logo HOT 1
- refactor: 优化 content script view 代码结构
- test: firefox 浏览器插件测试
- bug: workspace 支持点击跳转到对应目录的 Cargo.toml 文件 HOT 1
- bug: workspace 展开时会影响 sider 的 fixed HOT 1
- enhancement: 内容依赖path,repo 路径为 crate 包路径
- fix: workspace 路径为 . HOT 1
- bug: workspace * support HOT 3
- `docs.rs` 按钮的链接不正确 | 无链接或无法使用的情况下按钮应变成不可点击的状态 HOT 4
- 自动构建发布版本
- crate box 因为底部没有对齐 HOT 1
- not support Cargo workspace HOT 5
- the version problem (not std version) HOT 1
- refactor: next big version change HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cratehub.