Skip to content

Commit

Permalink
fix: add checks to not execute contentScript multiple times (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
amk-dev authored Jan 31, 2024
1 parent d0b6137 commit cd0c379
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 52 deletions.
2 changes: 1 addition & 1 deletion manifest.common.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Hoppscotch Browser Extension",
"version": "0.30",
"version": "0.31",
"description": "Provides more capabilities for Hoppscotch",
"icons": {
"16": "icons/icon-16x16.png",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hoppscotch-extension",
"version": "0.30.0",
"version": "0.31.0",
"description": "Provides more features to the Hoppscotch webapp (https://hoppscotch.io/)",
"scripts": {
"clean": "rimraf dist .parcel-cache",
Expand Down
115 changes: 66 additions & 49 deletions src/contentScript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
const fs = require("fs")

declare global {
interface Window {
HOPP_CONTENT_SCRIPT_EXECUTED: boolean
}
}

const hookContent = fs.readFileSync(__dirname + "/hookContent.js", {
encoding: "utf-8",
})
Expand All @@ -26,15 +32,6 @@ function getOriginList(): Promise<string[]> {
})
}

/**
* when an origin is added or removed,reevaluate the hook
*/
chrome.storage.onChanged.addListener((changes, _areaName) => {
if (changes.originList && changes.originList.newValue) {
injectHoppExtensionHook()
}
})

async function injectHoppExtensionHook() {
let originList = await getOriginList()

Expand All @@ -59,49 +56,69 @@ async function injectHoppExtensionHook() {
}
}

window.addEventListener("message", (ev) => {
if (ev.source !== window || !ev.data) {
function main() {
// check if the content script is already injected to avoid multiple injections side effects
if (window.HOPP_CONTENT_SCRIPT_EXECUTED) {
return
}

if (ev.data.type === "__POSTWOMAN_EXTENSION_REQUEST__") {
chrome.runtime.sendMessage(
{
messageType: "send-req",
data: ev.data.config,
},
(message) => {
if (message.data.error) {
window.postMessage(
{
type: "__POSTWOMAN_EXTENSION_ERROR__",
error: message.data.error,
},
"*"
)
} else {
window.postMessage(
{
type: "__POSTWOMAN_EXTENSION_RESPONSE__",
response: message.data.response,
isBinary: message.data.isBinary,
},
"*"
)
window.HOPP_CONTENT_SCRIPT_EXECUTED = true

/**
* when an origin is added or removed,reevaluate the hook
*/
chrome.storage.onChanged.addListener((changes, _areaName) => {
if (changes.originList && changes.originList.newValue) {
injectHoppExtensionHook()
}
})

window.addEventListener("message", (ev) => {
if (ev.source !== window || !ev.data) {
return
}

if (ev.data.type === "__POSTWOMAN_EXTENSION_REQUEST__") {
chrome.runtime.sendMessage(
{
messageType: "send-req",
data: ev.data.config,
},
(message) => {
if (message.data.error) {
window.postMessage(
{
type: "__POSTWOMAN_EXTENSION_ERROR__",
error: message.data.error,
},
"*"
)
} else {
window.postMessage(
{
type: "__POSTWOMAN_EXTENSION_RESPONSE__",
response: message.data.response,
isBinary: message.data.isBinary,
},
"*"
)
}
}
}
)
} else if (ev.data.type === "__POSTWOMAN_EXTENSION_CANCEL__") {
chrome.runtime.sendMessage({
messageType: "cancel-req",
})
}
})
)
} else if (ev.data.type === "__POSTWOMAN_EXTENSION_CANCEL__") {
chrome.runtime.sendMessage({
messageType: "cancel-req",
})
}
})

injectHoppExtensionHook()
injectHoppExtensionHook()

chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.action === "__POSTWOMAN_EXTENSION_PING__") {
sendResponse(true)
}
})
chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.action === "__POSTWOMAN_EXTENSION_PING__") {
sendResponse(true)
}
})
}

main()
2 changes: 1 addition & 1 deletion src/hookContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

window.__POSTWOMAN_EXTENSION_HOOK__ = {
getVersion: () => ({ major: 0, minor: 30 }),
getVersion: () => ({ major: 0, minor: 31 }),

decodeB64ToArrayBuffer: (input, ab) => {
const keyStr =
Expand Down

0 comments on commit cd0c379

Please sign in to comment.