have page script talking to controller
This commit is contained in:
+5
-4
@@ -2,11 +2,12 @@
|
|||||||
"name" : "Jack Russell",
|
"name" : "Jack Russell",
|
||||||
"version" : "0.1.0",
|
"version" : "0.1.0",
|
||||||
"description" : "Browser wallet extension for Aeternity",
|
"description" : "Browser wallet extension for Aeternity",
|
||||||
"icons" : {"48" : "icons/jrx-icon-48.png",
|
"icons" : {"48" : "art/jrx-icon-48.png",
|
||||||
"96" : "icons/jrx-icon-96.png"},
|
"96" : "art/jrx-icon-96.png"},
|
||||||
"content_scripts" : [{"matches" : ["*://*/*"],
|
"content_scripts" : [{"matches" : ["*://*/*"],
|
||||||
"js" : ["dist/content.js"]}],
|
"js" : ["dist/content.js"]}],
|
||||||
"browser_action" : {"default_icon" : "icons/jrx-icon-32.png",
|
"browser_action" : {"default_icon" : "art/jrx-icon-32.png",
|
||||||
"default_title" : "Jaeck Russell",
|
"default_title" : "Jæck Russell",
|
||||||
"default_popup" : "popup.html"},
|
"default_popup" : "popup.html"},
|
||||||
|
"background" : {"scripts": ["dist/background.js"]},
|
||||||
"permissions" : ["storage"]}
|
"permissions" : ["storage"]}
|
||||||
|
|||||||
+2
-2
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
<table width="100%" style="background: #dad1ff; border: 2px solid #6c5d96; border-radius: 5px;">
|
<table width="100%" style="background: #dad1ff; border: 2px solid #6c5d96; border-radius: 5px;">
|
||||||
<tr>
|
<tr>
|
||||||
<td><img src="./icons/jrx-icon-48.png" style="width: 32px; height: 32px;"></td>
|
<td><img src="./art/jrx-icon-48.png" style="width: 32px; height: 32px;"></td>
|
||||||
<td style="font-size: 32px; text-align: center; font-weight: bold">
|
<td style="font-size: 32px; text-align: center; font-weight: bold">
|
||||||
JÆCK RUSSELL
|
JÆCK RUSSELL
|
||||||
</td>
|
</td>
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
<div style="margin-top: 5px; width: 95%; background: #d9ffd6; border: 1px solid #6c5d96; border-radius: 5px;">
|
<div style="margin-top: 5px; width: 95%; background: #d9ffd6; border: 1px solid #6c5d96; border-radius: 5px;">
|
||||||
<table >
|
<table >
|
||||||
<tr>
|
<tr>
|
||||||
<td><img src="./ext/icons/keypair.svg" style="width: 32px; height: 32px;"></td>
|
<td><img src="./art/keypair.svg" style="width: 32px; height: 32px;"></td>
|
||||||
<td style="justify-content: center; font-size: 28px; text-align: center; font-weight: bold">
|
<td style="justify-content: center; font-size: 28px; text-align: center; font-weight: bold">
|
||||||
KEYPAIRS
|
KEYPAIRS
|
||||||
</td>
|
</td>
|
||||||
|
|||||||
@@ -0,0 +1,92 @@
|
|||||||
|
/**
|
||||||
|
* JR Controller
|
||||||
|
*
|
||||||
|
* Note that this is a *thread*. We put our business and storage logic within
|
||||||
|
* this thread. The reason we do that is we don't want to deal with the
|
||||||
|
* problem of conflicting concurrent writes..
|
||||||
|
*
|
||||||
|
* @module
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
export type {
|
||||||
|
jr_data,
|
||||||
|
named_keypair,
|
||||||
|
keypair
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
// top-level getters/setters
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data structure we store
|
||||||
|
*/
|
||||||
|
type jr_data
|
||||||
|
= {version : 0,
|
||||||
|
named_keypairs : Array<named_keypair>};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is the default {@link jr_data}.
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
let jr_data_mzero = {version : 0 as 0, // yay ts type inference can't infer 0 is of type 0
|
||||||
|
named_keypairs : []};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every keypair has to have a name
|
||||||
|
*/
|
||||||
|
type named_keypair
|
||||||
|
= {name : string}
|
||||||
|
& keypair;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* From NaCl
|
||||||
|
*/
|
||||||
|
type keypair
|
||||||
|
= {secretKey : Uint8Array,
|
||||||
|
publicKey : Uint8Array};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the state from the system
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
fetch_migrate
|
||||||
|
()
|
||||||
|
: Promise<jr_data>
|
||||||
|
{
|
||||||
|
// try to get data
|
||||||
|
let browser_data = await browser.storage.local.get('jr');
|
||||||
|
// test if it's there
|
||||||
|
if (!!(browser_data.jr))
|
||||||
|
return browser_data.jr;
|
||||||
|
// if the data is not there
|
||||||
|
else
|
||||||
|
return jr_data_mzero;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Put state
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
put
|
||||||
|
(state : jr_data)
|
||||||
|
: Promise<void>
|
||||||
|
{
|
||||||
|
await browser.storage.local.set({jr: state});
|
||||||
|
}
|
||||||
+32
-75
@@ -9,84 +9,41 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
export type {
|
|
||||||
jr_data,
|
|
||||||
named_keypair,
|
|
||||||
keypair
|
|
||||||
}
|
|
||||||
|
|
||||||
export {
|
|
||||||
// top-level getters/setters
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data structure we store
|
* Handle messages from the content script
|
||||||
*/
|
|
||||||
type jr_data
|
|
||||||
= {version : 0,
|
|
||||||
named_keypairs : Array<named_keypair>};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is the default {@link jr_data}.
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
let jr_data_mzero = {version : 0 as 0, // yay ts type inference can't infer 0 is of type 0
|
|
||||||
named_keypairs : []};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Every keypair has to have a name
|
|
||||||
*/
|
|
||||||
type named_keypair
|
|
||||||
= {name : string}
|
|
||||||
& keypair;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* From NaCl
|
|
||||||
*/
|
|
||||||
type keypair
|
|
||||||
= {secretKey : Uint8Array,
|
|
||||||
publicKey : Uint8Array};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the state from the system
|
|
||||||
*/
|
*/
|
||||||
async function
|
async function
|
||||||
fetch_migrate
|
bg_a2w_handler
|
||||||
()
|
(msg: any, sender: any, sendResponse: any)
|
||||||
: Promise<jr_data>
|
|
||||||
{
|
|
||||||
// try to get data
|
|
||||||
let browser_data = await browser.storage.local.get('jr');
|
|
||||||
// test if it's there
|
|
||||||
if (!!(browser_data.jr))
|
|
||||||
return browser_data.jr;
|
|
||||||
// if the data is not there
|
|
||||||
else
|
|
||||||
return jr_data_mzero;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Put state
|
|
||||||
*
|
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
async function
|
|
||||||
put
|
|
||||||
(state : jr_data)
|
|
||||||
: Promise<void>
|
: Promise<void>
|
||||||
{
|
{
|
||||||
await browser.storage.local.set({jr: state});
|
console.log('msg', msg);
|
||||||
|
console.log('sender', sender);
|
||||||
|
console.log('sendResponse', sendResponse);
|
||||||
|
// send them back to content script
|
||||||
|
// @ts-ignore stahp
|
||||||
|
//let the_sender: number = sender.tab.id;
|
||||||
|
//browser.tabs.sendMessage(the_sender, msg);
|
||||||
|
// @ts-ignore stahp
|
||||||
|
//sendResponse(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main function for background thread
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
jr_bg_main
|
||||||
|
()
|
||||||
|
: Promise<void>
|
||||||
|
{
|
||||||
|
console.log('hi');
|
||||||
|
|
||||||
|
// handle messages from content script
|
||||||
|
browser.runtime.onMessage.addListener(bg_a2w_handler);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
jr_bg_main();
|
||||||
|
|||||||
+76
-19
@@ -12,31 +12,88 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
jr_content_main();
|
|
||||||
|
let detect_msg = {type : "to_aepp",
|
||||||
|
data : {jsonrpc : "2.0",
|
||||||
|
method : "connection.announcePresence",
|
||||||
|
params : {id : "jr",
|
||||||
|
name : "JR",
|
||||||
|
networkId : "ae_uat",
|
||||||
|
origin : "foobar",
|
||||||
|
type : "extension"}}};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Spam detect message
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
spam_detect
|
||||||
|
()
|
||||||
|
{
|
||||||
|
while (true) {
|
||||||
|
window.postMessage(detect_msg);
|
||||||
|
await sleep(3000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hack from stack overflow somewhere to sleep for the given number of ms
|
||||||
|
*/
|
||||||
|
async function
|
||||||
|
sleep
|
||||||
|
(ms: number)
|
||||||
|
: Promise<void>
|
||||||
|
{
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Relays messages from page scripts to the background script
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
a2w_handler
|
||||||
|
(msg: {data: {type? : "to_aepp" | "to_waellet"}})
|
||||||
|
{
|
||||||
|
// branch
|
||||||
|
if ("to_waellet" === msg.data.type) {
|
||||||
|
browser.runtime.sendMessage(msg.data);
|
||||||
|
}
|
||||||
|
// otherwise ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Relays messages from background scripts to the page script
|
||||||
|
*/
|
||||||
|
function
|
||||||
|
w2a_handler
|
||||||
|
(msg: any)
|
||||||
|
{
|
||||||
|
window.postMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main function
|
||||||
|
*/
|
||||||
async function
|
async function
|
||||||
jr_content_main
|
jr_content_main
|
||||||
()
|
()
|
||||||
{
|
{
|
||||||
// @ts-ignore browser
|
// relay page messages back to the controller
|
||||||
browser.runtime.onMessage.addListener(handler);
|
window.addEventListener('message', a2w_handler);
|
||||||
window.addEventListener('message', page_script_message_handler);
|
|
||||||
|
|
||||||
|
// relay controller messages back to page
|
||||||
|
browser.runtime.onMessage.addListener(w2a_handler);
|
||||||
|
|
||||||
|
// spam detect
|
||||||
|
spam_detect();
|
||||||
/**
|
|
||||||
* This is the message that we spam the page script with when the user clicks
|
|
||||||
* the "make wallet detectable" button.
|
|
||||||
*
|
|
||||||
* Contains {@link detect_msg} as a field
|
|
||||||
*/
|
|
||||||
let detect_awcp_msg = {type : "to_aepp",
|
|
||||||
data : {jsonrpc : "2.0",
|
|
||||||
method : "connection.announcePresence",
|
|
||||||
params : detect_msg()}};
|
|
||||||
|
|
||||||
// mk detectable
|
|
||||||
mk_detectable(detect_awcp_msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
jr_content_main();
|
||||||
|
|||||||
Reference in New Issue
Block a user