|
1 | 1 | (function () { |
2 | 2 | try { |
3 | | - var settings = {}; |
| 3 | + var isExtensionOn = true; |
| 4 | + var settings = {}; |
| 5 | + |
| 6 | + function sendData(data) { |
| 7 | + if (!isExtensionOn) { |
| 8 | + return; |
| 9 | + } |
| 10 | + try { |
| 11 | + chrome.runtime.sendMessage(chrome.runtime.id, { "message": data }, function (response) { }); |
| 12 | + } catch (e) { } |
| 13 | + } |
4 | 14 |
|
5 | | - function escapeHtml(unsafe) { |
6 | | - return unsafe |
7 | | - .replace(/&/g, "&") |
8 | | - .replace(/</g, "<") |
9 | | - .replace(/>/g, ">") |
10 | | - .replace(/"/g, """) |
11 | | - .replace(/'/g, "'"); |
12 | | - } |
| 15 | + function escapeHtml(unsafe) { |
| 16 | + try { |
| 17 | + unsafe = unsafe || ""; |
| 18 | + if (settings.textonlymode) { |
| 19 | + return unsafe; |
| 20 | + } |
| 21 | + return unsafe |
| 22 | + .replace(/&/g, "&") |
| 23 | + .replace(/</g, "<") |
| 24 | + .replace(/>/g, ">") |
| 25 | + .replace(/"/g, """) |
| 26 | + .replace(/'/g, "'"); |
| 27 | + } catch (e) { |
| 28 | + return ""; |
| 29 | + } |
| 30 | + } |
13 | 31 |
|
14 | 32 | function getAllContentNodes(element) { |
15 | 33 | var resp = ""; |
|
18 | 36 | resp += getAllContentNodes(node); |
19 | 37 | } else if (node.nodeType === 3) { |
20 | 38 | resp += escapeHtml(node.textContent); |
21 | | - } else if (node.nodeType === 1) { |
22 | | - if (node.nodeName === "IMG" && node.src) { |
23 | | - resp += `<img src="${node.src}">`; |
24 | | - } else { |
25 | | - resp += node.outerHTML; |
26 | | - } |
27 | | - } |
| 39 | + } else if (node.nodeType === 1) { |
| 40 | + if (node.nodeName === "IMG" && node.src) { |
| 41 | + if (settings.textonlymode) { |
| 42 | + resp += escapeHtml(node.alt || ""); |
| 43 | + } else { |
| 44 | + resp += `<img src="${node.src}">`; |
| 45 | + } |
| 46 | + } else if (!settings.textonlymode) { |
| 47 | + resp += node.outerHTML; |
| 48 | + } |
| 49 | + } |
28 | 50 | }); |
29 | 51 | return resp; |
30 | 52 | } |
31 | 53 |
|
32 | 54 | function processMessage(ele) { |
33 | | - if (!ele) { |
34 | | - console.error("Element not found"); |
35 | | - return; |
36 | | - } |
37 | | - try { |
38 | | - if (ele.querySelector('img[alt*="User Joined"]')) { |
39 | | - console.log("User joined:", ele.textContent.trim()); |
40 | | - let data = {}; |
41 | | - data.chatmessage = ele.textContent.trim(); |
42 | | - data.event = "joined"; |
43 | | - data.type = "cherry"; |
44 | | - chrome.runtime.sendMessage(chrome.runtime.id, { "message": data }, function (response) { }); |
45 | | - return; |
46 | | - } |
| 55 | + if (!isExtensionOn) { |
| 56 | + return; |
| 57 | + } |
| 58 | + if (!ele || ele.nodeType !== 1) { |
| 59 | + console.error("Element not found"); |
| 60 | + return; |
| 61 | + } |
| 62 | + try { |
| 63 | + if (ele.querySelector('img[alt*="User Joined"]')) { |
| 64 | + if (!settings.capturejoinedevent) { |
| 65 | + return; |
| 66 | + } |
| 67 | + console.log("User joined:", ele.textContent.trim()); |
| 68 | + let data = {}; |
| 69 | + data.chatmessage = ele.textContent.trim(); |
| 70 | + data.event = "joined"; |
| 71 | + data.type = "cherrytv"; |
| 72 | + data.textonly = settings.textonlymode || false; |
| 73 | + sendData(data); |
| 74 | + return; |
| 75 | + } |
47 | 76 |
|
48 | 77 | if (ele.querySelector('img[alt*="Gift image"]')) { |
49 | 78 | var giftUser = ele.querySelector('.text-white.font-medium').textContent.trim(); |
|
80 | 109 |
|
81 | 110 | var data = {}; |
82 | 111 | data.chatname = chatName; |
83 | | - data.chatmessage = chatMessage; |
84 | | - data.type = "cherrytv"; |
85 | | - data.chatimg = chatImg; |
86 | | - data.textonly = false; |
87 | | - |
88 | | - chrome.runtime.sendMessage(chrome.runtime.id, { "message": data }, function (response) { }); |
89 | | - } catch (e) { |
90 | | - console.error("Error processing message:", e); |
91 | | - return; |
92 | | - } |
93 | | - } |
94 | | - |
95 | | - chrome.runtime.onMessage.addListener( |
96 | | - function (request, sender, sendResponse) { |
97 | | - try { |
| 112 | + data.chatmessage = chatMessage; |
| 113 | + data.type = "cherrytv"; |
| 114 | + data.chatimg = chatImg; |
| 115 | + data.textonly = settings.textonlymode || false; |
| 116 | + |
| 117 | + sendData(data); |
| 118 | + } catch (e) { |
| 119 | + console.error("Error processing message:", e); |
| 120 | + return; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + chrome.runtime.sendMessage(chrome.runtime.id, { "getSettings": true }, function(response) { |
| 125 | + if (typeof chrome !== "undefined" && chrome.runtime && chrome.runtime.lastError) { return; } |
| 126 | + response = response || {}; |
| 127 | + if ("settings" in response) { |
| 128 | + settings = response.settings || {}; |
| 129 | + } |
| 130 | + if ("state" in response) { |
| 131 | + isExtensionOn = response.state; |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + chrome.runtime.onMessage.addListener( |
| 136 | + function (request, sender, sendResponse) { |
| 137 | + try { |
98 | 138 | if ("getSource" == request){sendResponse("cherrytv"); return; } |
99 | 139 | if ("focusChat" == request) { |
100 | 140 | document.querySelector('input[data-test="field-message-box-public"]').focus(); |
101 | 141 | sendResponse(true); |
102 | 142 | return; |
103 | | - } |
104 | | - if (typeof request === "object") { |
105 | | - if ("settings" in request) { |
106 | | - settings = request.settings; |
107 | | - sendResponse(true); |
108 | | - return; |
109 | | - } |
110 | | - } |
111 | | - } catch (e) { } |
| 143 | + } |
| 144 | + if (typeof request === "object") { |
| 145 | + if ("state" in request) { |
| 146 | + isExtensionOn = request.state; |
| 147 | + } |
| 148 | + if ("settings" in request) { |
| 149 | + settings = request.settings || {}; |
| 150 | + sendResponse(true); |
| 151 | + return; |
| 152 | + } |
| 153 | + if ("state" in request) { |
| 154 | + sendResponse(true); |
| 155 | + return; |
| 156 | + } |
| 157 | + } |
| 158 | + } catch (e) { } |
112 | 159 | sendResponse(false); |
113 | 160 | } |
114 | 161 | ); |
|
0 commit comments