-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-fix-grammarly.js
122 lines (107 loc) · 3.38 KB
/
auto-fix-grammarly.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
var refreshTimer;
var lastValue = null;
var unchangedCount = 0;
var observer;
var active = true;
function refreshData() {
if (active) {
console.log("Running refreshData function");
try {
var itemRemove = document.querySelector(
'.cards-replacements_labels-itemRemove, ' + // Correctness
'.cards-replacements_labels-deleteAll, ' + // Clarity
'.cards-replacements_labels-itemInsert' // Engagement / Delivery
);
var dismissButton = document.querySelector('button[data-name="card/ignore"], button[data-name="card/bulk-accept-apply"]');
var updateAllButton = document.querySelector('button[data-name="card/update-all"]');
var actionable = false; // Flag to check if there are actions to perform
setTimeout(function() {
if (!active) {
return; // Check active status after delay
}
if (itemRemove) {
console.log("GrammarlyAutofix: Grammar mistake fixed!");
itemRemove.click();
actionable = true; // There is an action, so set flag to true
unchangedCount = 0; // Reset the count that checks if its finished
} else {
setTimeout(function() {
if (!active) {
return; // Check active status after delay
}
itemRemove = document.querySelector(
'.cards-replacements_labels-itemRemove, ' + // Correctness
'.cards-replacements_labels-deleteAll, ' + // Clarity
'.cards-replacements_labels-itemInsert' // Engagement / Delivery
);
if (itemRemove) {
console.log("GrammarlyAutofix: Found after delay. Grammar mistake fixed!");
itemRemove.click();
actionable = true;
unchangedCount = 0;
} else {
if (dismissButton) {
dismissButton.click();
console.log("GrammarlyAutofix: No solutions found.");
actionable = true;
unchangedCount = 0;
} else {
console.log("GrammarlyAutofix: 0 errors detected, waiting...");
actionable = false;
}
}
}, 250); // Retry delay
}
if (updateAllButton) {
console.log("Clicking updateAllButton");
updateAllButton.click();
actionable = true;
}
if (!actionable) {
unchangedCount++;
console.log("Unchanged Count: ", unchangedCount);
if (unchangedCount >= 10) {
clearTimeout(refreshTimer);
if (observer) {
observer.disconnect();
}
active = false;
alert("Success, zero errors! Stopping script.");
console.log("GrammarlyAutofix: Success, zero errors! Stopping script.");
return;
}
} else {
unchangedCount = 0;
}
}, 150); // Timeout
} catch (error) {
console.log("Error in refreshData: ", error);
}
refreshTimer = setTimeout(refreshData, 50);
}
else {
return;
}
}
function checkForChanges() {
console.log("Running checkForChanges function");
refreshData(); // Call refreshData to handle dynamic content
}
function startObserving() {
console.log("Starting to observe");
var targetNode = document.querySelector('.counterText'); // Adjust if a different parent element is more appropriate
if (!targetNode) {
console.log("Target node not found");
return;
}
var config = { childList: true, subtree: true };
observer = new MutationObserver(function(mutations) {
console.log("Mutation observed");
checkForChanges();
});
observer.observe(targetNode, config);
}
// Start the process
console.log("Starting the process");
refreshData();
startObserving();