141 lines
3.8 KiB
JavaScript
141 lines
3.8 KiB
JavaScript
|
console.log("boolean-algebra/outer.js injected successfully!");
|
||
|
|
||
|
const injectInnerScript = () => {
|
||
|
const s = document.createElement('script');
|
||
|
s.src = chrome.runtime.getURL('scripts/boolean-algebra/inner.js');
|
||
|
(document.head || document.documentElement).appendChild(s);
|
||
|
}
|
||
|
|
||
|
const replaceParens = (parens, counterParens, replacements, text) => {
|
||
|
parPattern = RegExp(`${parens[0]}.*${parens[1]}`);
|
||
|
|
||
|
while (parPattern.test(text)) {
|
||
|
const match = RegExp(parens[0], 'g').exec(text);
|
||
|
|
||
|
let i = match.index + match[0].length;
|
||
|
let parCount = 1;
|
||
|
while (parCount != 0) {
|
||
|
if (text[i] == counterParens[0]) parCount++;
|
||
|
if (text[i] == counterParens[1]) parCount--;
|
||
|
i++;
|
||
|
}
|
||
|
|
||
|
const secondPar = parens[1].replace(/\\(?!\\)/g, '').replace(/\\\\/,'\\');
|
||
|
|
||
|
text = text.substring(0, match.index)
|
||
|
+ replacements[0] + text.substring(match.index + match[0].length, i - secondPar.length)
|
||
|
+ replacements[1] + text.substring(i);
|
||
|
}
|
||
|
|
||
|
return text;
|
||
|
}
|
||
|
|
||
|
|
||
|
const parseExpression = (expression) => {
|
||
|
expression = replaceParens(["!\\(", "\\)"], ["(", ")"], ["\\overline{", "}"], expression);
|
||
|
expression = expression.replace(/!([A-Za-z01])/g, '\\overline{$1}');
|
||
|
return expression;
|
||
|
}
|
||
|
|
||
|
const insertExpression = (expression) => {
|
||
|
localStorage.setItem('boolean-expression', parseExpression(expression));
|
||
|
injectInnerScript();
|
||
|
}
|
||
|
|
||
|
const parseLatex = resultArray => {
|
||
|
const lines = [];
|
||
|
|
||
|
lines.push('\\begin{array}{ccl}');
|
||
|
|
||
|
for ([latex, comment] of resultArray) {
|
||
|
|
||
|
let l = latex;
|
||
|
|
||
|
l = replaceParens(['\\\\class{[-\\w]+}{', '}'], ['{', '}'], ['',''], l);
|
||
|
|
||
|
const lawTable = [
|
||
|
["Distributive", "Distributive law"],
|
||
|
["Demorgan", "Demorgan theorem"],
|
||
|
["Involution", "Involution law"],
|
||
|
["Distribution", "Distributive law"],
|
||
|
["Identity", "Identity law"],
|
||
|
["Complement", "Complement law"],
|
||
|
["Associative", "Associative law"],
|
||
|
["Idempotent", "Associative law"],
|
||
|
["Absorption", "Absorption law"],
|
||
|
];
|
||
|
|
||
|
let c = '&\\qquad&';
|
||
|
|
||
|
for (law of lawTable) {
|
||
|
if (comment.includes(law[0]))
|
||
|
c = ` &\\qquad& \\text{${law[1]}}`;
|
||
|
}
|
||
|
|
||
|
lines.push(`${l}${c} \\\\`);
|
||
|
}
|
||
|
|
||
|
lines.push('\\end{array}');
|
||
|
|
||
|
return lines.join('\n');
|
||
|
}
|
||
|
|
||
|
|
||
|
const whileFindReplace = (pattern, replacement, text) => {
|
||
|
while (pattern.test(text))
|
||
|
text = text.replace(pattern, replacement);
|
||
|
return text;
|
||
|
}
|
||
|
|
||
|
const convertToPropositionalLogic = latex => {
|
||
|
|
||
|
// replace neg
|
||
|
latex = replaceParens(['\\\\overline{', '}'], ['{', '}'], ['\\neg ', ''], latex);
|
||
|
|
||
|
// replace and
|
||
|
latex = whileFindReplace(/\b(?!\\cup|\\cap|\\text|\\quad)([A-Z0-9]|\\overline{.+})([A-Z0-9]|\\overline{.+?})(?=[\s\+])/, '($1 \\wedge $2)', latex);
|
||
|
|
||
|
// // replace or
|
||
|
latex = latex.replace(/\+/g, ' \\vee ');
|
||
|
|
||
|
return latex;
|
||
|
}
|
||
|
|
||
|
const convertToSetTheory = latex => {
|
||
|
|
||
|
// replace and
|
||
|
latex = whileFindReplace(/\b(?!\\cup|\\cap|\\text|\\quad)([A-Z0-9]|\\overline{.+})([A-Z0-9]|\\overline{.+?})(?=[\s\+])/, '($1 \\cap $2)', latex);
|
||
|
|
||
|
// // replace or
|
||
|
latex = latex.replace(/\+/g, ' \\cup ');
|
||
|
|
||
|
return latex;
|
||
|
}
|
||
|
|
||
|
const getLatex = () => {
|
||
|
return parseLatex(JSON.parse(localStorage.getItem('boolean-result')));
|
||
|
}
|
||
|
|
||
|
const timeout = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
||
|
|
||
|
const calculateExpression = async (expression) => {
|
||
|
console.log("Inserting expression");
|
||
|
insertExpression(expression);
|
||
|
|
||
|
await timeout(2);
|
||
|
return getLatex();
|
||
|
}
|
||
|
|
||
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||
|
console.log("Checking Message Type");
|
||
|
if (message.type !== "bool_alg") return;
|
||
|
|
||
|
calculateExpression(message.content.expression)
|
||
|
.then(latex => sendResponse({
|
||
|
content: message.content.outputType === 'logic' ? convertToPropositionalLogic(latex) :
|
||
|
message.content.outputType === 'set' ? convertToSetTheory(latex) :
|
||
|
latex
|
||
|
}));
|
||
|
|
||
|
return true;
|
||
|
});
|