forked from hoppscotch/hoppscotch-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: extension identification improvements (hoppscotch#136)
Co-authored-by: Andrew Bastin <[email protected]>
- Loading branch information
1 parent
90892b2
commit 2f8be0b
Showing
9 changed files
with
576 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module.exports = { | ||
semi: false, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
Oops, something went wrong.