diff --git a/jrx/src/b_lib.ts b/jrx/src/b_lib.ts index 45a34cf..eee194f 100644 --- a/jrx/src/b_lib.ts +++ b/jrx/src/b_lib.ts @@ -70,6 +70,7 @@ export { p_get_data, p_generate_keypair, p_get_unique_name, + p_activate_key, // background script api b_main }; @@ -161,7 +162,7 @@ p_get_unique_name () : Promise { - console.log('b_lib p_generate_keypair'); + console.log('b_lib p_get_unique_name'); return await browser.runtime.sendMessage({frum: 'popup', data: {method : 'p_get_unique_name', @@ -169,6 +170,22 @@ p_get_unique_name } +/** + * Activate given key + */ +async function +p_activate_key + (name: string) + : Promise +{ + console.log('b_lib p_activate_key'); + + return await browser.runtime.sendMessage({frum: 'popup', + data: {method : 'p_activate_key', + args : {name: name}}}) as void; +} + + //============================================================================= //============================================================================= // API: BACKGROUND SCRIPT @@ -901,6 +918,7 @@ type bi_popup_calldata = bi_pc_p_get_data | bi_pc_p_generate_keypair | bi_pc_p_get_unique_name + | bi_pc_p_activate_key ; type bi_pc_p_get_data @@ -915,6 +933,10 @@ type bi_pc_p_get_unique_name = {method : 'p_get_unique_name', args : {}}; +type bi_pc_p_activate_key + = {method : 'p_activate_key', + args : {name: string}}; + type bi_popup_return_data = p_data | void @@ -943,6 +965,8 @@ bi_msg_handler_popup return await bi_p_generate_keypair(msg.args); case 'p_get_unique_name': return await bi_p_get_unique_name(msg.args); + case 'p_activate_key': + return await bi_p_activate_key(msg.args); default: throw new Error('unsupported popup call method'); } @@ -1068,6 +1092,37 @@ bi_name_unique } +async function +bi_p_activate_key + (args: {name: string}) + : Promise +{ + console.log('b_lib bi_p_activate_key ' + args.name); + + // get state + let istate : bi_state = await bi_get_state(); + + // find key with name + // type bi_rich_keypair + // = {nacl_keypair : bi_nacl_keypair, + // name : string, + // active : boolean}; + // type bi_state + // = {rich_keypairs: Array}; + let new_rich_keypairs : Array = []; + for (let this_irkp of istate.rich_keypairs) { + // if this is the name, set active = true + if (args.name === this_irkp.name) + this_irkp.active = true; + else + this_irkp.active = false; + new_rich_keypairs.push(this_irkp); + } + + let new_state : bi_state = {rich_keypairs: new_rich_keypairs}; + await bi_set_state(new_state); +} + diff --git a/jrx/src/popup.ts b/jrx/src/popup.ts index 6d0074d..9c5f252 100644 --- a/jrx/src/popup.ts +++ b/jrx/src/popup.ts @@ -137,37 +137,25 @@ pi_repop // if active, make background green if (this_rkp.active) this_li.style.background = 'green'; - else + // otherwise make background red and add activate li + else { this_li.style.background = 'red'; - - // // balance - // // add a br - // let br = document.createElement('br'); - // this_li.appendChild(br); - - // // add balance - // this_li.innerHTML += 'Balance: '; - // let maybe_balance = await pi_balance(this_akstr); - // // if there is a balance, show it in green - // if (maybe_balance.ok) - // { - // let greentext = document.createElement('span'); - // greentext.style.color = 'green'; - // // @ts-ignore fuck off i know what i'm doing - // greentext.innerHTML = '' + maybe_balance.balance; - - // this_li.appendChild(greentext); - // } - // // if not, show reason and make it red - // else - // { - // let redtext = document.createElement('span'); - // redtext.style.color = 'red'; - // // @ts-ignore fuck off i know what i'm doing - // redtext.innerHTML = '' + maybe_balance.reason; - - // this_li.appendChild(redtext); - // } + let activate_li = document.createElement('li'); + let activate_a = document.createElement('a'); + activate_a.innerHTML = 'Switch to this key'; + activate_a.style.color = 'blue'; + activate_a.style.textDecoration = 'underline'; + // dirty but so is your mind so go fuck yourself + activate_a.onclick = + async function () { + // tell controller that we want to activate this key + await b_lib.p_activate_key(this_rkp.name); + await pi_repop(); + }; + // add li and a + activate_li.appendChild(activate_a); + rkp_ul.appendChild(activate_li); + } // add it to the list pubkeys_ul.appendChild(this_li);