-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from HunnySajid/feat/temp-firefox
DO NOT MERGE: Feat/temp firefox
- Loading branch information
Showing
12 changed files
with
318 additions
and
72 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
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,105 @@ | ||
import { pubsub } from "./pubsub"; | ||
|
||
var extensionId = ""; | ||
|
||
window.addEventListener( | ||
"message", | ||
async (event) => { | ||
// Accept messages only from same window | ||
if (event.source !== window) { | ||
return; | ||
} | ||
|
||
if (event.data.type && event.data.type === "signify-extension") { | ||
console.log("Content script loaded from polaris-web"); | ||
extensionId = event.data.data.extensionId; | ||
pubsub.publish("signify-extension-loaded", extensionId); | ||
} | ||
|
||
if (event.data.type && event.data.type === "signify-signature") { | ||
pubsub.publish("signify-signature", event.data.data); | ||
} | ||
}, | ||
false | ||
); | ||
|
||
const requestAid = () => { | ||
window.postMessage({ type: "select-identifier" }, "*"); | ||
}; | ||
|
||
const requestCredential = () => { | ||
window.postMessage({ type: "select-credential" }, "*"); | ||
}; | ||
|
||
const requestAidORCred = () => { | ||
window.postMessage({ type: "select-aid-or-credential" }, "*"); | ||
}; | ||
|
||
const requestAutoSignin = async () => { | ||
window.postMessage( | ||
{ | ||
type: "fetch-resource", | ||
subtype: "auto-signin-signature", | ||
}, | ||
"*" | ||
); | ||
// const { data, error } = await chrome.runtime.sendMessage(extensionId, { | ||
// type: "fetch-resource", | ||
// subtype: "auto-signin-signature", | ||
// }); | ||
// if (error) { | ||
// window.postMessage({ type: "select-auto-signin" }, "*"); | ||
// } else { | ||
// pubsub.publish("signify-signature", data); | ||
// } | ||
}; | ||
|
||
const isExtensionInstalled = (func) => { | ||
const timeout = setTimeout(() => { | ||
func(false); | ||
}, 1000); | ||
pubsub.subscribe("signify-extension-loaded", (_event, data) => { | ||
func(data); | ||
clearTimeout(timeout); | ||
pubsub.unsubscribe("signify-extension-loaded"); | ||
}); | ||
}; | ||
|
||
const trySettingVendorUrl = async (vendorUrl) => { | ||
window.postMessage( | ||
{ | ||
type: "vendor-info", | ||
subtype: "attempt-set-vendor-url", | ||
data: { | ||
vendorUrl, | ||
}, | ||
}, | ||
"*" | ||
); | ||
// await chrome.runtime.sendMessage(extensionId, { | ||
// type: "vendor-info", | ||
// subtype: "attempt-set-vendor-url", | ||
// data: { | ||
// vendorUrl, | ||
// }, | ||
// }); | ||
}; | ||
|
||
const subscribeToSignature = (func) => { | ||
pubsub.subscribe("signify-signature", (_event, data) => func(data)); | ||
}; | ||
|
||
const unsubscribeFromSignature = () => { | ||
pubsub.unsubscribe("signify-signature"); | ||
}; | ||
|
||
export { | ||
requestAid, | ||
requestCredential, | ||
requestAidORCred, | ||
requestAutoSignin, | ||
subscribeToSignature, | ||
unsubscribeFromSignature, | ||
isExtensionInstalled, | ||
trySettingVendorUrl, | ||
}; |
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,51 @@ | ||
export const pubsub = (() => { | ||
const events = {}; | ||
|
||
let subscribersId = -1; | ||
|
||
function publish(event, data) { | ||
if (!events[event]) { | ||
return false; | ||
} | ||
|
||
const subscribers = events[event]; | ||
subscribers.forEach((subscriber) => { | ||
subscriber.func(event, data); | ||
}); | ||
return true; | ||
} | ||
|
||
function subscribe(event, func) { | ||
if (!events[event]) { | ||
events[event] = []; | ||
} | ||
|
||
subscribersId += 1; | ||
const token = subscribersId.toString(); | ||
events[event].push({ | ||
token, | ||
func, | ||
}); | ||
return token; | ||
} | ||
|
||
function unsubscribe(token) { | ||
const found = Object.keys(events).some((event) => | ||
events[event].some((subscriber, index) => { | ||
const areEqual = subscriber.token === token.toString(); | ||
if (areEqual) { | ||
events[event].splice(index, 1); | ||
} | ||
return areEqual; | ||
}) | ||
); | ||
|
||
return found ? token : null; | ||
} | ||
|
||
return { | ||
publish, | ||
subscribe, | ||
unsubscribe, | ||
}; | ||
})(); |
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
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
Oops, something went wrong.