Get Started with Vue
quick guide about using @crxjs/vite-plugin to build chrome extension, after which you have to make some changes as to run the project smoothly.
Vite and HMR
Only npm run dev
may not be all, you have to set the hmr port,
Use Typescript
remember import global definition is an important step. When building the project follow @crxjs/vite-plugin
guide please choose Typescript option, after which import @types/chrome
node_module.
some changes to tsconfig.json
,
Use Vue in content_scripts
There is not direct way to do so, but you can mount Vue app to some newly created element which just appended to some existed element of websites, which content_scripts shall run at.
create element, and mounte Vue app,
Message Communication
The simple way of message communication is use chrome.runtime
apis. Client send out message by chrome.runtime.sendMessage
, and Server listen on chrome.runtime.onMessage.addListener
. Normally message listeners run at background,
but you can set message listeners in content_scripts, and some changes have to be make, Client should specify the target browser active tab,
Another notice, messages do not fan out, just on copy, in other word, you should not set up two listeners in the same content_scripts, you should combine them all together.
How to Parse Response
Be careful of the cors
thing, for example, Douban.vue
runs at book.douban.com
website page, it can’t just use fetch
to fetch szlib.org.cn
xhr responses, where that’s background and message communications come to the stage. However, when only the HTML response we can get from target webiste, we would not parse them in background. Because background don’t have DOMParser
, and yeah content_script or popups have, the solution is obvious, messages to background, fetch and response back the origin html response, and the parse html response in content_script or popups by DOMParser
.
Use stores
Combines Vue app store and Chrome extension storage,