-
Notifications
You must be signed in to change notification settings - Fork 0
/
britannica__cleanUI.js
137 lines (127 loc) · 5.12 KB
/
britannica__cleanUI.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
// ==UserScript==
// @name Clean Britannica Dictionary UI
// @namespace https://github.com/zica87/self-made-userscipts
// @version 1.0
// @description Clear elements at the top and add some buttons.
// @author zica
// @match https://www.britannica.com/*
// @grant none
// @license GPL-3.0
// ==/UserScript==
(function () {
"use strict";
clear_elements();
tweak_elements();
move_searchbar_and_entries_to_right();
const fileType = "mp3"; // choose between mp3, ogg, and wav
add_pronounce_button();
generate_toggle_all_examples_button();
function clear_elements() {
document.getElementById("wrap_cr_t").remove(); // ad at the top
// document.getElementById("wrap_c").style.margin = 0; // space on the left & right
document.getElementsByClassName("section-links")[0].remove(); // top sections
document.getElementById("main_title").remove(); // "The Britannica Dictionary"
document.getElementById("ld_entries_v2_mainh").remove(); // the word
const all_lines = document.getElementsByClassName("dline"); // Britannica Dictionary definition of ...
const n = all_lines.length;
// bcz all_lines changes immediately when we change it, we need to use this way of iterating to properly remove all
for (let i = n - 1; i >= 0; --i) {
all_lines[i].remove();
}
}
function tweak_elements() {
// short(in height) search box
document.getElementsByClassName("ld_search_inp")[1].style.padding = "6px";
// search button position
Object.assign(document.getElementsByClassName("search_btn")[1].style, {
top: "8px",
right: "7px",
});
// short list box if it doesn't have that many items
Object.assign(document.getElementsByClassName("o_list")[0].style, {
height: "revert",
maxHeight: "72px",
});
document.getElementById("ld_entries_v2_all").style.marginTop = 0; // margin at the top
// highlight definitions
for (const definition of document.getElementsByClassName("def_text")) {
Object.assign(definition.style, {
backgroundColor: "gray",
color: "white",
padding: "0 0.5em",
});
}
}
function move_searchbar_and_entries_to_right() {
const entries = document.getElementById("ld_entries_v2_others_block");
Object.assign(entries.style, {
backgroundColor: "white"
});
const searchbar = document.getElementsByClassName("desktop_top")[0];
const rightSide = document.getElementById("wrap_cr_br");
Object.assign(searchbar.style, {
width: getComputedStyle(rightSide).getPropertyValue("width"),
});
const toolbar = document.createElement("div");
Object.assign(toolbar.style, {
position: "sticky",
top: 0,
zIndex: Number.MAX_SAFE_INTEGER,
});
toolbar.append(searchbar);
toolbar.append(entries);
rightSide.prepend(toolbar);
Object.assign(rightSide.style, {
height: getComputedStyle(rightSide.parentElement).getPropertyValue("height"),
});
}
function generate_toggle_all_examples_button() {
const toggleAllExamples = document.createElement("input");
Object.assign(toggleAllExamples.style, {
borderRadius: "10px",
position: "fixed",
zIndex: Number.MAX_SAFE_INTEGER,
bottom: "10vh",
right: "5vh",
});
let toShow = true;
Object.assign(toggleAllExamples, {
type: "button",
value: "show all examples",
onclick: function () {
for (const wrapper of document.getElementsByClassName("vi_more")) {
if (toShow !== wrapper.classList.contains("opened")) {
wrapper.firstElementChild.click();
}
}
toShow = !toShow;
if (toShow) {
this.value = "show all examples";
} else {
this.value = "hide all examples";
}
},
});
document.documentElement.prepend(toggleAllExamples);
}
function add_pronounce_button() {
for (const icon of document.getElementsByClassName("play_pron")) {
const path = icon.dataset.lang.replace('_', '/');
const file = icon.dataset.file;
const dir = icon.dataset.dir;
const url = `https://media.merriam-webster.com/audio/prons/${path}/${fileType}/${dir}/${file}.${fileType}`;
const audio = new Audio(url);
const pronounce = document.createElement("input");
Object.assign(pronounce.style, {
borderRadius: "10px",
marginLeft: "1em"
});
Object.assign(pronounce, {
type: "button",
value: "pronounce",
onclick: audio.play.bind(audio)
});
icon.after(pronounce);
}
}
})();