Document directory
- Examples of front-end UI and script snippets
- Userscript example
Difference between extension and userscript
It has been observed that the userscript in chrome belongs to a special extension; it belongs to the contentscript section in extension and does not have its own backgroundpage, so its functions are limited.
The key issue is that you cannot directly debug it. it is inconvenient to open a breakpoint or output it to the console each time.
Make a simple repl
Event Notification is a feasible way to allow the foreground page to communicate with contentscript through custom events on the Dom.
There are several ways to share data, such as localstorage, cookie, and Dom dataset. The last one is the most convenient. There is no data size limit and the page will not be adversely affected when exiting.
The purpose of this example is to directly input a script snippet to understand the environment status in contentscript or execute functions in contentscript.
Examples of front-end UI and script snippets
In actual applications, the UI for debugging should be dynamically built in userscript.
The sharing event method is document. createevent ('event') to create an event, and then initialize and publish the event.
In this way, the front-end page and contentscript can notify each other.
Userscript example
* Principle: directly generate dynamic scripts or convert functions to strings for further execution
In turn, there are two ways for userscript to notify the front-end page:
- Insert the external script or dynamic creation script, corresponding to the above requirescript and globaleval. Be careful! Note: This will affect the global environment of the front-end page. If you need to introduce jquery in this way, remember to use jquery. noconfict to return the global variable jquery.
- You can also use shared event notifications.