-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwarning-remove.js
152 lines (138 loc) · 5.1 KB
/
warning-remove.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* list of default strings to remove */
let defaultStrings = [
"Foundry Virtual Tabletop requires a minimum screen resolution",
"not displayed because the game Canvas is disabled",
"is unmaintained and may introduce stability issues"
];
let removeStrings = []
const W_R_ID = 'warning-remove';
Hooks.on('ready', () => {
game.settings.register(W_R_ID, 'qty', {
name: 'Number of messages to remove',
hint: 'default: 3',
scope: 'client',
requiresReload: true,
default: '3',
type: Number,
config: true
});
const qty = game.settings.get(W_R_ID, 'qty');
for (let s = 0; s < (qty); s++) {
let defaultString = "Enter a long Exact message";
switch (s) {
case 0:
defaultString = defaultStrings[0];
break;
case 1:
defaultString = defaultStrings[1];
break;
case 2:
defaultString = defaultStrings[2];
break;
}
game.settings.register(W_R_ID, 'string_' + s, {
name: 'Message to remove #' + (s + 1),
hint: 'Enter the text or part of the text in the message you want to remove, try to enter as much text is possible to avoid removing useful messages. The text must be case sensitive',
scope: 'client',
requiresReload: true,
default: defaultString,
type: String,
config: true
});
let savedString = game.settings.get(W_R_ID, 'string_' + s);
if (savedString && savedString.length > 0) {
removeStrings.push(game.settings.get(W_R_ID, 'string_' + s));
}
}
// get the final string array
removeStrings = foundry.utils.mergeObject(defaultStrings, removeStrings);
// reset module
game.settings.register(W_R_ID, "resetModule", {
name: "Reset default options",
hint: "Select 'Reset' and save to go back to the module original settings.",
scope: "client",
config: true,
requiresReload: false,
type: String,
choices: {
"a": "No",
"b": "Reset"
},
default: "a",
onChange: value => {
if (value == 'a') {
return;
}
const clientStorage = game.settings.storage.get("client");
const worldStorage = game.settings.storage.get("world");
for (const clientKey of Object.keys(clientStorage)) {
if (clientKey.startsWith(W_R_ID)) clientStorage.removeItem(clientKey);
}
for (const worldSetting of worldStorage) {
if (worldSetting.key.startsWith(W_R_ID)) worldSetting.delete();
}
window.location.reload();
}
});
});
/**
* This function initializes a Proxy to intercept changes to the `ui.notifications.queue` array.
* It accepts an array of strings to match against any notification that would be added to the queue.
* If a notification matches, the Proxy will modify it to prevent it from logging anything to the
* console, and to hide it in the UI.
*
* This function should be run during the "ready" Hook call.
*/
function InitNotificationsProxy(patternsToHide = []) {
// Do nothing if no patterns are provided.
if (!patternsToHide.length) { return; }
// Get the original array of queued notifications, and store a constant reference to it
const notificationQueue = ui.notifications.queue;
// Convert the provided patterns into regular expressions
const regExpPatterns = patternsToHide.map((pattern) => new RegExp(pattern));
// Define a handler for the proxy that will be used to intercept notifications
const handler = {
set: function (target, property, value) {
// Handle changes to the array length property
if (property === "length") {
// Perform the default behavior for length changes
target.length = value;
return true; // Indicate success
}
// Handle directly setting the value for non-index properties (necessary for array methods like 'next')
else if (typeof property === "string" && isNaN(Number(property))) {
// Perform the default behavior for non-index properties.
target[property] = value;
return true; // Indicate success
}
// Handle setting array indices
else if (!isNaN(Number(property))) {
// If the value is a notification and its content matches one of the provided patterns ...
if (value
&& typeof value === "object"
&& "message" in value
&& typeof value.message === "string"
&& regExpPatterns.some((pattern) => pattern.exec(value.message))) {
// ... edit the notification to:
Object.assign(value, {
console: false, // ... prevent logging it to the console
permanent: false, // ... ensure the notification element is removed automatically
type: "do-not-display" // ... 'hack' the type to add the 'do-not-display' class
});
}
// Otherwise, perform the default behavior for setting index properties.
target[Number(property)] = value;
return true; // Indicate success
}
return false; // Indicate failure for all other cases
}
};
// Replace the notifications queue array with a Proxy defined by the above handler.
ui.notifications.queue = new Proxy(notificationQueue, handler);
}
// Initialize the notifications proxy during the 'ready' hook, after ui.notifications has been defined
Hooks.once("ready", () => {
// I've hard-coded the two notifications I want to hide, but this could easily be a
// user setting, allowing users to customize which notifications are silenced.
InitNotificationsProxy(removeStrings);
});