forked from glennraya/vscode-settings-json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvscode-script.js
99 lines (80 loc) · 3.66 KB
/
vscode-script.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
const TARGET_DIV_CLASS = '.monaco-workbench';
const COMMAND_CENTER_DIV_CLASS = '.quick-input-widget';
const BLUR_BACKGROUND_DIV_ID = 'command-blur';
document.addEventListener('DOMContentLoaded', () => {
const checkMonacoWorkbench = setInterval(() => {
const targetDiv = document.querySelector(TARGET_DIV_CLASS);
if (!targetDiv) {
return;
}
// Clear the interval once the targetDiv is found.
clearInterval(checkMonacoWorkbench);
// Observer to listen to children being added/removed from targetDiv.
const targetObserver = new MutationObserver((targetMutations) => {
for (const parentMutation of targetMutations) {
if (parentMutation.type !== 'childList') {
continue;
}
const commandCenterDiv = document.querySelector(COMMAND_CENTER_DIV_CLASS);
// Go to the next iteration if the command dialog is not found.
if (!commandCenterDiv) {
continue;
}
// Disconnect the child list observer once the command dialog is found.
targetObserver.disconnect();
// Handle the initial open of the command center.
handleCommandCenterOpen();
// Observer to detect when the command center is opened/closed.
const commandCenterObserver = new MutationObserver((commandCenterMutations) => {
for (const mutation of commandCenterMutations) {
if (mutation.type !== 'attributes') {
continue;
} else if (mutation.attributeName !== 'style') {
continue;
}
// @ts-expect-error • Style exists but can't be inferred.
if (commandCenterDiv.style.display === 'none') {
// If the command center `display` style is set to
// `none`, remove the blurred background.
handleCommandCenterClose();
} else {
// Otherwise, add the blurred background.
handleCommandCenterOpen();
}
}
});
commandCenterObserver.observe(commandCenterDiv, {
attributes: true,
});
}
});
targetObserver.observe(targetDiv, {
childList: true,
});
}, 100); // Check every 100ms
function handleCommandCenterOpen() {
const targetDiv = document.querySelector(TARGET_DIV_CLASS);
// Delete the existing element if it already exists.
const existingElement = document.getElementById(BLUR_BACKGROUND_DIV_ID);
if (existingElement) {
existingElement.remove();
}
// Create and configure the new element.
const backgroundDiv = document.createElement('div');
backgroundDiv.setAttribute('id', BLUR_BACKGROUND_DIV_ID);
// Add an event listener to remove the element on clicked.
backgroundDiv.addEventListener('click', function () {
backgroundDiv.remove();
});
// Append the new element as a child of the targetDiv.
targetDiv?.appendChild(backgroundDiv);
}
// Remove the backdrop blur.
function handleCommandCenterClose() {
const element = document.getElementById(BLUR_BACKGROUND_DIV_ID);
// Trigger the click handler that will remove the element.
if (element) {
element.click();
}
}
});