/
contentScript.js
127 lines (108 loc) · 4.96 KB
/
contentScript.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const header = document.querySelector('.stream-chat-header');
if (header) {
header.parentNode.removeChild(header);
}
const CRMButtonToInsert = `<div class="Layout-sc-1xcs6mc-0" style="margin-left:5px">
<div class="InjectLayout-sc-1i43xsx-0 kBtJDm viewer-card-drag-cancel">
<div class="Layout-sc-1xcs6mc-0 cajBtn">
<div class="Layout-sc-1xcs6mc-0"><button id="twitch-crm-button"
class="ScCoreButton-sc-ocjdkq-0 ScCoreButtonPrimary-sc-ocjdkq-1 bTXTVH gmCwLG">
<div class="ScCoreButtonLabel-sc-s7h2b7-0 gPDjGr">
<div data-a-target="tw-core-button-label-text" class="Layout-sc-1xcs6mc-0 bFxzAY">CRM
</div>
</div>
</button></div>
</div>
</div>
</div>`
function addTwitchCRMButton () {
const viewerCard = document.getElementById('VIEWER_CARD_ID');
if (!viewerCard) return;
const twitchCRMPrefix = 'twitch-crm-';
const username = viewerCard.querySelector('a.tw-link').textContent;
const nameForStorage = `${twitchCRMPrefix}${username}`;
viewerCard.querySelectorAll('div[data-a-target="tw-core-button-label-text"]').forEach(function(element) {
if (element.textContent.trim() === "Invite") {
let parent = element.parentElement;
while (parent) {
// Get all direct children
let childDivs = Array.from(parent.children).filter(child => child.tagName === 'DIV');
if (parent.tagName.toLowerCase() === 'div' && childDivs.length >= 4) {
// If the last child div's text is CRM, then break.
if(childDivs[childDivs.length-1].textContent.trim() === 'CRM') {
break;
}
console.log('Adding a CRM button...');
// Adding the HTML snippet as the last child
parent.insertAdjacentHTML('beforeend', CRMButtonToInsert);
// Find the div with twitch-crm-button id, and assign a listener that gets triggered when clicked
const crmButton = document.getElementById('twitch-crm-button');
// Add click listener
crmButton.addEventListener('click', () => {
// Retrieve the info we have about username from localStorage
const existingInfo = localStorage.getItem(nameForStorage) ? localStorage.getItem(nameForStorage) : "still a mystery to be discovered...";
// Instead of using alert, we need to use prompt to say:
// What we know about {username} is: <something here>
// If you'd like to update it, submit the new info below.
const newChatterInfo = prompt(`What we know about ${username} is: ${existingInfo} \n\nIf you'd like to update it, submit the new info below.`, existingInfo);
// Save the updated info back to localStorage
if (newChatterInfo) {
localStorage.setItem(nameForStorage, newChatterInfo);
}
});
break; // Exit the loop as we found the target parent
}
parent = parent.parentElement;
}
}
});
}
// Function to handle dynamic content like popups
function handleDynamicContent() {
// Example: Re-run blur function after a popup is opened.
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
mutation.addedNodes.forEach((node) => {
addTwitchCRMButton(node);
});
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
handleDynamicContent(); // Listen for dynamic content like popups
function saveBackup() {
const twitchCRMPrefix = 'twitch-crm-';
const allUsersWithValues = Object.keys(localStorage).reduce((acc, key) => {
if (key.startsWith(twitchCRMPrefix)) {
acc[key] = localStorage.getItem(key);
}
return acc;
}, {});
// Convert the result to JSON
const json = JSON.stringify(allUsersWithValues);
// Create a Blob with the JSON content
const blob = new Blob([json], {type: 'application/json'});
// Create a link and trigger the download
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'twitchCRMBackup.json'; // Name of the file to be downloaded
document.body.appendChild(a); // Append the link to the body (required for Firefox)
a.click();
document.body.removeChild(a); // Clean up
}
document.addEventListener('keydown', function(event) {
// For macOS: event.metaKey, for Windows/Linux: event.ctrlKey
const isCmdOrCtrl = event.metaKey || event.ctrlKey;
const isOption = event.altKey;
const isSKey = event.key === 's' || event.key === 'S' || event.key === 'ß';
if (isCmdOrCtrl && isOption && isSKey) {
event.preventDefault(); // Prevent any default behavior
// Place the command you want to run here
saveBackup();
}
});