hoppscotch / hoppscotch-extension

🧩 Browser extensions to provide more capabilities to https://hoppscotch.io
https://hoppscotch.io
MIT License
254 stars 95 forks source link

fix: add checks to not execute contentScript multiple times #263

Closed amk-dev closed 8 months ago

amk-dev commented 8 months ago

Fixes HFE-419 Closes hoppscotch/hoppscotch#1426

Before

  1. Hoppscotch Website and Hoppscotch Extension communicate by sending events. So when the website wants to send a request through the extension, it sends an event with the request data. The extension listens for this event and sends the request. but our content script gets registered multiple times due to the behaviour of onUpdated tab event. this causes the event listener to be registered multiple times, so when the website sends the event, the event handler gets called multiple times, which causes the request to be sent multiple times.

  2. As mentioned in the above point, the tabs.onUpdated's behaviour is a the root cause of this issue. This is because the tabs.onUpdated event fires for various changes

    • Title changes
    • Favicon changes
    • Audio playing on the tab + Other small changes

because of this we cant rely on onUpdated to fire only for url changes like we want. there are newer apis like registerContentScript and getRegisteredContentScripts, which could help us register the content script only once, but they're not supported in the minimum version of chrome that we support.

After

We introduced a global variable HOPP_CONTENT_SCRIPT_EXECUTED to track if the content script has been executed for a tab. if it has been executed, we dont execute it again. this prevents multiple event handlers from being registered for the same event. this works because all content scripts share the same global scope.