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.
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.
Fixes HFE-419 Closes hoppscotch/hoppscotch#1426
Before
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.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
because of this we cant rely on onUpdated to fire only for url changes like we want. there are newer apis like
registerContentScript
andgetRegisteredContentScripts
, 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.