使用plasmo开发浏览器插件的时候,有时候需要在指定网站的指定页面添加自定义的UI内容,如果通过content.js内容脚本去通过js创建的话,可就太麻烦了,要写不少的js代码。不过plasmo已经帮我们实现了这个功能,就是Content Scripts UI,官方地址:Content Scripts UI – Plasmo
创建内容UI脚本
在contents里面创建文件:plasmo.tsx
可以单独匹配对应的网站,可以初始化监听更新UI位置,可以获取渲染位置的UI:
import type {
PlasmoCSConfig,
PlasmoGetOverlayAnchor,
PlasmoWatchOverlayAnchor,
} from 'plasmo'
export const config: PlasmoCSConfig = {
matches: ['https://www.plasmo.com/*'],
}
// watch page ui change,then reset ui position
export const watchOverlayAnchor: PlasmoWatchOverlayAnchor = (
updatePosition
) => {
const interval = setInterval(() => {
updatePosition()
}, 420)
return () => clearInterval(interval)
}
// get ui render position
export const getOverlayAnchor: PlasmoGetOverlayAnchor = async () =>
document.querySelector(`header > div > a[href="/"]`)
// ui components
const PlasmoPricingExtra =