Contact and debug contentscript and userscript in chrome

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.