This repository was archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearchable-dropdowns.user.js
More file actions
106 lines (95 loc) · 2.56 KB
/
searchable-dropdowns.user.js
File metadata and controls
106 lines (95 loc) · 2.56 KB
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
// ==UserScript==
// @name Searchable Dropdowns for Scratch 3
// @version 0.1.2
// @namespace https://github.com/forkphorus/cat-plus
// @match https://scratch.mit.edu/projects/*
// @grant GM_addStyle
// ==/UserScript==
function callback(mutationList) {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
const newNodes = mutation.addedNodes;
for (const node of newNodes) {
if (node.classList && node.classList.contains('blocklyDropdownMenu')) {
addSearch();
break;
}
}
}
}
}
function addSearch() {
const el = document.createElement('input');
el.type = 'text';
el.addEventListener('input', search);
el.classList.add('u-dropdown-searchbar');
const container = getDropDownMenu();
container.insertBefore(el, container.firstChild);
el.focus();
for (const child of getItems()) {
child.dataset.visible = 'true';
}
}
function search(e) {
const value = e.target.value.toLowerCase();
for (const child of getItems()) {
const text = child.textContent.toLowerCase();
const contains = text.includes(value);
child.dataset.visible = contains.toString();
}
}
let cachedDropDownContentElement = null;
function getDropDownContentElement() {
if (cachedDropDownContentElement) {
return cachedDropDownContentElement;
}
cachedDropDownContentElement = document.querySelector('.blocklyDropDownContent');
return cachedDropDownContentElement;
}
function getDropDownMenu() {
return getDropDownContentElement().querySelector('.blocklyDropdownMenu')
}
function getItems() {
const el = getDropDownMenu();
if (el) {
return Array.from(el.children).filter((child) => child.tagName !== 'INPUT');
}
return [];
}
const observer = new MutationObserver(callback);
observer.observe(document.body, {
childList: true,
subtree: true,
});
GM_addStyle(`
.u-dropdown-searchbar {
/* seems to always fill the entire thing without making a horizontal scrollbar */
width: calc(100% - 20px);
/* based on styles for the title input */
color: white;
background-color: hsla(0, 100%, 100%, 0.25);
border: 1px dashed hsla(0, 0%, 0%, 0.15);
padding: .5rem;
outline: none;
transition: 0.25s ease-out;
font-size: 13px;
font-weight: bold;
border-radius: 4px;
}
.u-dropdown-searchbar:hover {
background-color: hsla(0, 100%, 100%, 0.5);
}
.u-dropdown-searchbar:focus {
background-color: white;
color: black;
}
[data-visible="false"] {
display: none;
}
/*.goog-menuitem-highlight {
background: transparent;
}
.goog-menuitem:hover {
background: rgba(0, 0, 0, 0.2);
}*/
`);