-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-bidi.js
62 lines (52 loc) · 1.35 KB
/
add-bidi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* global browser */
const tagsToProcess = new Set([
'H1',
'H2',
'H3',
'H4',
'H5',
'H6',
'P',
'A',
'INPUT',
'TEXTAREA',
'LABEL',
'PRE',
'UL',
'OL',
]);
const cssSelectorToProcess = Array.from(tagsToProcess)
.map((tag) => `${tag.toLowerCase()}:not([dir])`)
.join(', ');
const addBidiSupport = (element) => {
element.setAttribute('dir', 'auto');
const textAlignStyle = window.getComputedStyle(element)['text-align'];
if (textAlignStyle === 'left') {
element.setAttribute('style', 'text-align: start');
}
};
const addBidiSupportToExisitingElements = () => {
const allElements = document.querySelectorAll(cssSelectorToProcess);
allElements.forEach((element) => addBidiSupport(element));
};
const main = async () => {
const { active } = await browser.storage.local.get('active');
if (!active) return;
addBidiSupportToExisitingElements();
const observer = new MutationObserver((mutationsList) => {
mutationsList.forEach((mutation) => {
mutation.addedNodes.forEach((addedNode) => {
if (addedNode.nodeType === Node.ELEMENT_NODE) {
addedNode.querySelectorAll(cssSelectorToProcess).forEach((element) => {
addBidiSupport(element);
});
}
});
});
});
observer.observe(
document.querySelector('body'),
{ childList: true, subtree: true },
);
};
main();