Skip to content

Commit

Permalink
Support ordinary URL
Browse files Browse the repository at this point in the history
  • Loading branch information
ninegua committed Oct 1, 2021
1 parent f50227c commit d1e4bd4
Showing 1 changed file with 87 additions and 57 deletions.
144 changes: 87 additions & 57 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ async function decode_to_json(data) {
}
}
// data is a string by now
if (data.startsWith("http")) {
if (data.startsWith("https://p5deo-6aaaa-aaaab-aaaxq-cai")) {
// strip URL when it is meant for this app
let i = data.indexOf("/?msg=");
if (i > 0) {
return decode_to_json(decodeURIComponent(data.substring(i + 6)));
}
} else {
// Or return a normal URL
return { content: data };
}
}
try {
// try decode base64
data = atob(data);
Expand All @@ -111,6 +123,7 @@ async function decode_to_json(data) {
return decode_to_json(Buffer.from(data, "binary"));
}
} catch (e) {}
// decode JSON
try {
var json = JSON.parse(data);
if (Array.isArray(json)) {
Expand Down Expand Up @@ -192,76 +205,93 @@ async function prepare_send(message) {
pre.innerText = "Unsupported message format";
return;
}
const ingress = Cbor.decode(fromHexString(content));
if (!ingress || !ingress.content) {
pre.innerText = "Unsupported message format";
return;
var action = "Send";
// shortcut for normal URL
if (content == content + "" && content.startsWith("http")) {
pre.innerText = content;
action = "Go";
} else {
const ingress = Cbor.decode(fromHexString(content));
if (!ingress || !ingress.content) {
pre.innerText = "Unsupported message format";
return;
}
const canister_id = new Principal(ingress.content.canister_id).toString();
const args = await try_decode(
canister_id,
ingress.content.method_name,
ingress.content.arg,
false
);
const text =
"Request type : " +
ingress.content.request_type +
"\nSender : " +
new Principal(ingress.content.sender).toString() +
"\nCanister id : " +
canister_id +
"\nMethod name : " +
ingress.content.method_name +
"\nArguments : " +
(typeof args == "string" ? args : "(" + stringify(args, null, 2) + ")");
pre.innerText = text;
}
const canister_id = new Principal(ingress.content.canister_id).toString();
const args = await try_decode(
canister_id,
ingress.content.method_name,
ingress.content.arg,
false
);
const text =
"Request type : " +
ingress.content.request_type +
"\nSender : " +
new Principal(ingress.content.sender).toString() +
"\nCanister id : " +
canister_id +
"\nMethod name : " +
ingress.content.method_name +
"\nArguments : " +
(typeof args == "string" ? args : "(" + stringify(args, null, 2) + ")");
pre.innerText = text;
var button = document.getElementById("send");
if (!button) {
button = document.createElement("button");
button.id = "send";
}
button.innerHTML = "Send";
button.innerHTML = action;
button.onclick = do_send(message);
result.appendChild(button);
}

function do_send(message) {
return async () => {
const send_button = document.getElementById("send");
send_button.disabled = true;
send_button.innerHTML = "Sent";
const result = document.getElementById("result");
const pre = document.createElement("pre");
pre.id = "status";
const copy_button = document.createElement("button");
copy_button.id = "copy";
copy_button.innerHTML = "Copy";
copy_button.style.display = "none";
copy_button.onclick = () => {
const txt = document.createElement("textarea");
txt.value = pre.innerText;
txt.setAttribute("readonly", "");
txt.style = { position: "absolute", left: "-9999px" };
result.appendChild(txt);
txt.select();
document.execCommand("copy");
result.removeChild(txt);
if (
message.content &&
message.content == message.content + "" &&
message.content.startsWith("http")
) {
return () => {
window.location.href = message.content;
};
result.appendChild(pre);
result.appendChild(copy_button);
const update_status = (reply, replied) => {
var quote = (s) => s;
if (replied) {
copy_button.style.display = "block";
} else {
quote = (s) => "Waiting for response: " + s;
}
let text = typeof reply == "string" ? reply : stringify(reply, null, 2);
pre.innerText = quote(text);
} else {
return async () => {
const send_button = document.getElementById("send");
send_button.disabled = true;
send_button.innerHTML = "Sent";
const result = document.getElementById("result");
const pre = document.createElement("pre");
pre.id = "status";
const copy_button = document.createElement("button");
copy_button.id = "copy";
copy_button.innerHTML = "Copy";
copy_button.style.display = "none";
copy_button.onclick = () => {
const txt = document.createElement("textarea");
txt.value = pre.innerText;
txt.setAttribute("readonly", "");
txt.style = { position: "absolute", left: "-9999px" };
result.appendChild(txt);
txt.select();
document.execCommand("copy");
result.removeChild(txt);
};
result.appendChild(pre);
result.appendChild(copy_button);
const update_status = (reply, replied) => {
var quote = (s) => s;
if (replied) {
copy_button.style.display = "block";
} else {
quote = (s) => "Waiting for response: " + s;
}
let text = typeof reply == "string" ? reply : stringify(reply, null, 2);
pre.innerText = quote(text);
};
await send_message(message, update_status, sleep);
};
await send_message(message, update_status, sleep);
};
}
}

function sleep(ms) {
Expand Down

0 comments on commit d1e4bd4

Please sign in to comment.