简体   繁体   中英

Tracking URL for chrome extension

I am developing a chrome extension for which i need to track if the URL requested by the user matches a URL from a default list. If it does, then I must redirect to a different page.

In my current implementation, I use a timer to periodically check the url of the page with the elements in the list. But this makes it slow and unclean because the page has to load first. I know that security reasons prevent the capture of outgoing http requests. Instead, is there anyway, I can read the url of the incoming page ?

Also, instead of using a timer to check periodically, can I detect the event of the request being made ?

Thank you.

You can use the chrome.webRequest API

onBeforeRequest (optionally synchronous) - Fires when a request is about to occur. This event is sent before any TCP connection is made and can be used to cancel or redirect requests.

You will need to utilize the onBeforeRequest event to listen to requests. This will also eliminate the need for periodic checking as per your existing implementation.

Also, here's a similar SO question which you can use as an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM