Skip to content

Commit

Permalink
Use .mjs files
Browse files Browse the repository at this point in the history
  • Loading branch information
HashidaTKS committed Sep 24, 2024
1 parent e496e46 commit 6e3490e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 70 deletions.
4 changes: 3 additions & 1 deletion src/web/dialog.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" type="application/javascript" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="dialog.js" type="application/javascript"></script>
<script src="recipient-parser.mjs" type="module"></script>
<script src="recipient-classifier.mjs" type="module"></script>
<script src="dialog.js" type="module"></script>
<link href="dialog.css" rel="stylesheet">
</head>
<body>
Expand Down
74 changes: 5 additions & 69 deletions src/web/dialog.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,4 @@
function parse(recipient) {
const address = /<([^@]+@[^>]+)>\s*$/.test(recipient) ? RegExp.$1 : recipient;
const domain = address.split("@")[1].toLowerCase();
return {
recipient,
address,
domain,
};
}

class RecipientClassifier {
constructor({ internalDomains } = {}) {
const uniquePatterns = new Set(
(internalDomains || [])
.filter((pattern) => !pattern.startsWith("#")) // reject commented out items
.map(
(pattern) =>
pattern
.toLowerCase()
.replace(/^(-?)@/, "$1") // delete needless "@" from domain only patterns: "@example.com" => "example.com"
.replace(/^(-?)(?![^@]+@)/, "$1*@") // normalize to full address patterns: "[email protected]" => "[email protected]", "example.com" => "*@example.com"
)
);
const negativeItems = new Set(
[...uniquePatterns].filter((pattern) => pattern.startsWith("-")).map((pattern) => pattern.replace(/^-/, ""))
);
for (const negativeItem of negativeItems) {
uniquePatterns.delete(negativeItem);
uniquePatterns.delete(`-${negativeItem}`);
}
this.$internalPatternsMatcher = new RegExp(
`^(${[...uniquePatterns].map((pattern) => this.$toRegExpSource(pattern)).join("|")})$`,
"i"
);
this.classify = this.classify.bind(this);
}

$toRegExpSource(source) {
// https://stackoverflow.com/questions/6300183/sanitize-string-of-regex-characters-before-regexp-build
const sanitized = source.replace(/[#-.]|[[-^]|[?|{}]/g, "\\$&");

const wildcardAccepted = sanitized.replace(/\\\*/g, ".*").replace(/\\\?/g, ".");

return wildcardAccepted;
}

classify(recipients) {
const internals = new Set();
const externals = new Set();

for (const recipient of recipients) {
const classifiedRecipient = {
...parse(recipient),
};
const address = classifiedRecipient.address;
if (this.$internalPatternsMatcher.test(address)) internals.add(classifiedRecipient);
else externals.add(classifiedRecipient);
}

return {
internals: Array.from(internals),
externals: Array.from(externals),
};
}
}
import { RecipientClassifier } from "./recipient-classifier.mjs";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
Office.initialize = function (reason) {};
Expand All @@ -84,7 +20,7 @@ function sendStatusToParent(status) {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function onCheckAllTrusted() {
window.onCheckAllTrusted = () => {
const checkTargetLength = $("fluent-checkbox.check-target").length;
const checkedTargetLength = $("fluent-checkbox.check-target.checked").length;
const toBeCheckedNumber = $("#trusted-domains fluent-checkbox.check-target").not(".checked").length;
Expand All @@ -94,17 +30,17 @@ function onCheckAllTrusted() {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function onOk() {
window.onOk = () => {
sendStatusToParent("ok");
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function onCancel() {
window.onCancel = () => {
sendStatusToParent("cancel");
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
function checkboxChanged(target_element) {
window.checkboxChanged = (target_element) => {
const checkTargetLength = $("fluent-checkbox.check-target").length;
const checkedTargetLength = $("fluent-checkbox.check-target.checked").length;
// If the target is currently checked, the target is unchecked after this function and vice versa.
Expand Down

0 comments on commit 6e3490e

Please sign in to comment.