Skip to content

Commit

Permalink
feat: add background message listener
Browse files Browse the repository at this point in the history
  • Loading branch information
HunnySajid committed Dec 22, 2023
1 parent 462a4ac commit 2ede179
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 85 deletions.
75 changes: 43 additions & 32 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,54 @@
import { randomPasscode, SignifyClient, Tier, ready } from 'signify-ts'
const url = "https://keria-dev.rootsid.cloud/admin"
const boot_url = "https://keria-dev.rootsid.cloud"
import { userService } from "@pages/background/services/user";
import { configService } from "@pages/background/services/config";

console.log('Background script loaded');

// Handle messages
chrome.runtime.onMessage.addListener(
async function (message, sender, sendResponse) {
if (sender.tab) {
// Handle mesages from content script
console.log("Message received from content script at " + sender.tab.url + ": " + message.type)
if (message.type) {
switch (message.type) {
case "getVersion":
const manifestData = chrome.runtime.getManifest()
sendResponse(manifestData.version);
break;
case "isUnlocked":
const [tab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
const response = chrome.tabs.sendMessage(tab.id!, {type: "for_content_script"});
// sendResponse(true);
break;
case "hasLogin":
sendResponse({ login: "AID 123" });
break;
case "authenticate":
sendResponse({ signature: "ABCD" });
break;
default:
break;
function (message, sender, sendResponse) {
(async () => {
if (sender.tab) {
// Handle mesages from content script
console.log("Message received from content script at " + sender.tab.url + ": " + message.type)
if (message.type) {
switch (message.type) {
case "getVersion":
const manifestData = chrome.runtime.getManifest()
sendResponse(manifestData.version);
break;
case "isUnlocked":
const [tab] = await chrome.tabs.query({active: true, lastFocusedWindow: true});
const response = chrome.tabs.sendMessage(tab.id!, {type: "for_content_script"});
// sendResponse(true);
break;
case "hasLogin":
sendResponse({ login: "AID 123" });
break;
case "authenticate":
sendResponse({ signature: "ABCD" });
break;
default:
break;
}
}

} else if (sender.origin === "chrome-extension://" + chrome.runtime.id &&
sender.url === "chrome-extension://" + chrome.runtime.id + "/src/pages/popup/index.html") {
// handle messages from Popup
console.log("Message received from browser extension pupup: " + message)
if(message.type === "authentication" && message.subtype === "persist-token"){
await userService.setToken(message.passcode)
await configService.setUrl(message.vendorUrl)
const token = await userService.getToken()
console.log("token", token);
}
console.log("sender", sender);
sendResponse({ resp: "received" });
}
})();

} else if (sender.origin === "chrome-extension://" + chrome.runtime.id &&
sender.url === "chrome-extension://" + chrome.runtime.id + "/src/pages/popup/index.html") {
// handle messages from Popup
console.log("Message received from browser extension pupup: " + message)
sendResponse({ resp: "received" });
}
// Important! Return true to indicate you want to send a response asynchronously
return true;
}
);

122 changes: 69 additions & 53 deletions src/pages/popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,72 +1,88 @@
import React from 'react';
import logo from '@assets/img/128_keri_logo.png';
import { randomPasscode, SignifyClient, Tier, ready } from 'signify-ts'
const url = "https://keria-dev.rootsid.cloud/admin"
const boot_url = "https://keria-dev.rootsid.cloud"
import React, { useState, useEffect } from "react";
import { userService } from "@pages/background/services/user";
import { Signin } from "@components/forms/signin";
import { Main } from "@components/main";

// import { randomPasscode, SignifyClient, Tier, ready } from 'signify-ts'
const url = "https://keria-dev.rootsid.cloud/admin";
const boot_url = "https://keria-dev.rootsid.cloud";

export default function Popup(): JSX.Element {
const [passcode, setPasscode] = React.useState<string>("");
const [isAuthenticated, setIsAuthenticated] = useState(false);

const checkAuthentication = async () => {
const token = await userService.getToken();
if (token) {
document.body.style.width = "900px";
} else {
document.body.style.width = "300px";
}
setIsAuthenticated(!!token);
};

const [passcode, setPasscode] = React.useState<string>('')
const [client, setClient] = React.useState<SignifyClient | undefined>(undefined)
useEffect(() => {
checkAuthentication();
}, []);
// const [client, setClient] = React.useState<SignifyClient | undefined>(undefined)

React.useEffect(() => {
ready().then(() => {
console.log("signify client is ready")
})
}
, [])
// React.useEffect(() => {
// ready().then(() => {
// console.log("signify client is ready")
// })
// }
// , [])

const generatePasscode = async () => {
let p = randomPasscode()
setPasscode(p)
const response = await chrome.runtime.sendMessage({message: "passcode generated"});
console.log(response)
}
// const generatePasscode = () => {
// let p = randomPasscode()
// setPasscode(p)
// }

const newClient = (passcode: string) => {
ready().then(() => {
console.log("signify client is ready")
}).catch((err) => {
console.log("signify client is not ready")
})
const client = new SignifyClient(url, passcode, Tier.low, boot_url);
console.log(client)
setClient(client)
}
// const newClient = (passcode: string) => {
// ready().then(() => {
// console.log("signify client is ready")
// }).catch((err) => {
// console.log("signify client is not ready")
// })
// const client = new SignifyClient(url, passcode, Tier.low, boot_url);
// console.log(client)
// setClient(client)
// }

const [bootedState, setBootedState] = React.useState<string>('')
const [bootedState, setBootedState] = useState<string>("");

const boot = async (client: SignifyClient) => {
await client.boot()
let resp = await client.state()
console.log('booted client')
setBootedState('booted')
return JSON.stringify(resp)
}
// const boot = async (client: SignifyClient) => {
// await client.boot()
// let resp = await client.state()
// console.log('booted client')
// setBootedState('booted')
// return JSON.stringify(resp)
// }

const [connectedState, setConnectedState] = React.useState<string>('')
const [connectedState, setConnectedState] = React.useState<string>("");

const connect = async (client: SignifyClient) => {
await client.connect()
let resp = await client.state()
console.log('connected client')
setConnectedState('connected')
return JSON.stringify(resp)
}
// const connect = async (client: SignifyClient) => {
// await client.connect()
// let resp = await client.state()
// console.log('connected client')
// setConnectedState('connected')
// return JSON.stringify(resp)
// }

return (
<div className="absolute top-0 left-0 right-0 bottom-0 text-center h-full p-3 bg-gray-800">
<header className="flex flex-col items-center justify-center text-white">
<img src={logo} className="w-32 h-32" alt="logo" />
<button onClick={generatePasscode}> generate</button>
<p>p:{passcode}</p>
<button onClick={() => newClient(passcode)}>set client</button>
{client && <p>client:{client.agent?.pre}</p>}
{/* <header className="flex flex-col items-center justify-center text-white"> */}
{/* <img src={logo} className="w-32 h-32" alt="logo" /> */}
{/* <button onClick={generatePasscode}> generate</button> */}
{/* <p>p:{passcode}</p> */}
{/* <button onClick={() => newClient(passcode)}>set client</button> */}
{/* {client && <p>client:{client.agent?.pre}</p>}
{client && <button onClick={() => boot(client!)}>boot</button>}
<p>bootedState:{bootedState}</p>
{client && <button onClick={() => connect(client!)}>connect</button>}
<p>connectedState:{connectedState}</p>
</header>
<p>connectedState:{connectedState}</p> */}
{/* </header> */}
{isAuthenticated ? <Main /> : <Signin />}
</div>
);
}

0 comments on commit 2ede179

Please sign in to comment.