Browse Source

closes #2 | Title observe

master
Rei 8 months ago
parent
commit
e66fac7b98
1 changed files with 32 additions and 1 deletions
  1. 32
    1
      main.js

+ 32
- 1
main.js View File

@@ -1,5 +1,25 @@
1 1
 const classname = "no-caps-addon-activated";
2 2
 
3
+// Restrict the amount of times the titleChange callback will be called
4
+// This is to prevent an endless loop of title changes if the page itself
5
+// also listens to title-changes to modify it.
6
+// 100 title changes should be enough for most pages as most pages
7
+// don't change the title that often
8
+const maxTitleChanges = 100;
9
+let titleChanges = 0;
10
+
11
+const titleChangeCallback = mutations => {
12
+  mutations.forEach(mutation => {
13
+    // Prevent calling the callback when changing the title to lowercase
14
+    if (
15
+      titleChanges++ < maxTitleChanges &&
16
+      mutation.target.textContent != mutation.target.textContent.toLowerCase()
17
+    ) {
18
+      document.title = document.title.toLowerCase();
19
+    }
20
+  });
21
+};
22
+
3 23
 const initialize = ([{ urls }, { listType }]) => {
4 24
   let inList = 0;
5 25
   // If urls is empty there is no url blocked
@@ -18,11 +38,22 @@ const initialize = ([{ urls }, { listType }]) => {
18 38
 
19 39
   // if blocklist && not in list
20 40
   // if allowlist && in list
21
-  if (inList === listType) {
41
+  if (listType !== undefined ? inList === listType : !inList) {
22 42
     document.documentElement.classList.add(classname);
23 43
 
24 44
     // Set the title to lowercase
25 45
     document.title = document.title.toLowerCase();
46
+
47
+    // Observe the title for changes
48
+    const titleElement = document.getElementsByTagName("title")[0];
49
+
50
+    const observer = new MutationObserver(titleChangeCallback);
51
+
52
+    observer.observe(titleElement, {
53
+      subtree: true,
54
+      characterData: true,
55
+      childList: true
56
+    });
26 57
   }
27 58
 };
28 59
 let urls = browser.storage.local.get("urls");

Loading…
Cancel
Save