Skip to content

Commit

Permalink
feat: extension identification improvements (hoppscotch#136)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Bastin <[email protected]>
  • Loading branch information
amk-dev and AndrewBastin authored May 13, 2022
1 parent 90892b2 commit 2f8be0b
Show file tree
Hide file tree
Showing 9 changed files with 576 additions and 406 deletions.
3 changes: 3 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
semi: false,
};
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Hoppscotch Browser Extension",
"version": "0.23",
"version": "0.24",
"description": "Provides more capabilities for Hoppscotch (https://hoppscotch.io)",
"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.23.0",
"version": "0.24.0",
"description": "Provides more features to the Hoppscotch webapp (https://hoppscotch.io/)",
"scripts": {
"clean": "rimraf dist",
Expand Down
118 changes: 83 additions & 35 deletions src/contentScript.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,99 @@
const fs = require("fs")

const hookContent = fs.readFileSync(__dirname + "/hookContent.js", {
encoding: "utf-8"
});
encoding: "utf-8",
})

window.addEventListener('message', ev => {
const hookContentInvalidOrigin = fs.readFileSync(
__dirname + "/hookContentInvalidOrigin.js",
{
encoding: "utf-8",
}
)

type HookType = "valid_origin" | "unknown-origin"

function getOriginList(): Promise<string[]> {
return new Promise((resolve, reject) => {
chrome.storage.sync.get((items) => {
let originList: string[] = JSON.parse(items["originList"])

resolve(originList)
})
})
}

/**
* 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()

let url = new URL(window.location.href)

const script = document.createElement("script")
script.textContent = originList.includes(url.origin)
? hookContent
: hookContentInvalidOrigin
document.documentElement.appendChild(script)
script.parentNode.removeChild(script)
}

window.addEventListener("message", (ev) => {
if (ev.source !== window || !ev.data) {
return;
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
}, '*');
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__') {
)
} else if (ev.data.type === "__POSTWOMAN_EXTENSION_CANCEL__") {
chrome.runtime.sendMessage({
messageType: "cancel-req"
});
messageType: "cancel-req",
})
}
});

const VERSION = { major: 0, minor: 23 };
})

const script = document.createElement('script');
script.textContent = hookContent;
const VERSION = { major: 0, minor: 24 }

document.documentElement.appendChild(script);
script.parentNode.removeChild(script);
console.log(
`Connected to Hoppscotch Browser Extension v${VERSION.major}.${VERSION.minor}`
)

console.log(`Connected to Hoppscotch Browser Extension v${VERSION.major}.${VERSION.minor}`);
injectHoppExtensionHook()

chrome.runtime.onMessage.addListener((msg, _sender, sendResponse) => {
if (msg.action === '__POSTWOMAN_EXTENSION_PING__') {
sendResponse(true);
if (msg.action === "__POSTWOMAN_EXTENSION_PING__") {
sendResponse(true)
}
});
})
4 changes: 1 addition & 3 deletions src/defaultOrigins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export const DEFAULT_ORIGIN_LIST = [
"https://hoppscotch.io"
];
export const DEFAULT_ORIGIN_LIST = ["https://hoppscotch.io"]
Loading

0 comments on commit 2f8be0b

Please sign in to comment.