98 lines
2.4 KiB
JavaScript
98 lines
2.4 KiB
JavaScript
|
|
/* ------------------------------ Chrome Stuff ------------------------------ */
|
|
|
|
const openAndSend = ({url, message, timeout, callback=() => {}}) =>
|
|
chrome.tabs.create({url: url})
|
|
.then(tab =>
|
|
setTimeout(() =>
|
|
chrome.tabs.sendMessage(tab.id, message, callback),
|
|
timeout
|
|
));
|
|
|
|
/* ----------------------------- Boolean Algebra ---------------------------- */
|
|
|
|
const openBoolean = msg =>
|
|
openAndSend({
|
|
url: "https://boolean-algebra.com",
|
|
message: {type: "bool_alg", content: msg},
|
|
timeout: 5000,
|
|
callback: (res) =>
|
|
openAndSend({
|
|
url: "pages/answer/answer.html",
|
|
message: {type: "bool_alg_ans", content: res.content},
|
|
timeout: 1000,
|
|
})
|
|
});
|
|
|
|
/* ----------------------------- Induction Proof ---------------------------- */
|
|
|
|
openInduction = msg =>
|
|
openAndSend({
|
|
url: "https://www.wolframalpha.com/input/?i=" + encodeURIComponent(msg.expression),
|
|
message: {type: "induction", content: msg},
|
|
timeout: 15000,
|
|
callback: (res) =>
|
|
openAndSend({
|
|
url: "pages/answer/answer.html",
|
|
message: {type: "induction_ans", content: res.content},
|
|
timeout: 1000,
|
|
})
|
|
});
|
|
|
|
/* ------------------------ Permutation, Combination ------------------------ */
|
|
|
|
const fac = (n) => {
|
|
let val=1;
|
|
for (let i = 2; i <= n; i++)
|
|
val = val * i;
|
|
return val;
|
|
}
|
|
|
|
const perm = (n, r) => fac(n) / fac(n - r);
|
|
|
|
const comb = (n, r) => fac(n) / (fac(n - r) * fac(r));
|
|
|
|
const openPerm = msg =>
|
|
openAndSend({
|
|
url: "pages/answer/answer.html",
|
|
message: {type: "perms_ans", content: `\\[ \\nPr{${msg.n}}{${msg.r}} = ${perm(msg.n, msg.r)} \\]`},
|
|
timeout: 1000,
|
|
});
|
|
|
|
const openComb = msg =>
|
|
openAndSend({
|
|
url: "pages/answer/answer.html",
|
|
message: {type: "combs_ans", content: `\\[ \\nCr{${msg.n}}{${msg.r}} = ${comb(msg.n, msg.r)} \\]`},
|
|
timeout: 1000,
|
|
})
|
|
|
|
|
|
/* ----------------------------- Message switch ----------------------------- */
|
|
|
|
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
console.log("Message recieved!");
|
|
console.log("Checking message type!");
|
|
|
|
switch (message.type) {
|
|
case "bool_alg":
|
|
openBoolean(message.content);
|
|
break;
|
|
|
|
case "induction":
|
|
openInduction(message.content);
|
|
break;
|
|
|
|
case "perm":
|
|
openPerm(message.content);
|
|
break;
|
|
|
|
case "comb":
|
|
openComb(message.content);
|
|
break;
|
|
|
|
default:
|
|
console.error("Couldn't recognize message!");
|
|
break;
|
|
}
|
|
}); |