-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiscuss-elastic-options.user.js
More file actions
146 lines (119 loc) · 5.2 KB
/
discuss-elastic-options.user.js
File metadata and controls
146 lines (119 loc) · 5.2 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
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
// ==UserScript==
// @name Discuss Elastic Options
// @namespace http://stackoverflow.com/users/982924/rasg
// @author RASG
// @description Add options to discuss.elastic.co to (1) expand code box, (2) resize code font size, (3) expand answer box
// @require http://code.jquery.com/jquery.min.js
// @require https://raw.github.com/odyniec/MonkeyConfig/master/monkeyconfig.js
// @require https://cdn.jsdelivr.net/npm/siiimple-toast/dist/siiimple-toast.min.js
// @resource toastcss https://cdn.jsdelivr.net/npm/siiimple-toast/dist/style.css
// @include http*://discuss.elastic.co/*
// @icon https://www.google.com/s2/favicons?domain=discuss.elastic.co
// @version 2020.01.17.1221
// @grant GM_addStyle
// @grant GM_getMetadata
// @grant GM_getResourceText
// @grant GM_getValue
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// @run-at document-start
// @noframes
// ==/UserScript==
// -----------------------------------------------------------------------------
// LOAD TOAST NOTIFICATIONS LIBRARY
// -----------------------------------------------------------------------------
// @require https://cdn.jsdelivr.net/npm/siiimple-toast/dist/siiimple-toast.min.js
// @resource toastcss https://cdn.jsdelivr.net/npm/siiimple-toast/dist/style.css
// @grant GM_addStyle
// @grant GM_getResourceText
GM_addStyle( GM_getResourceText("toastcss") );
var toast = siiimpleToast.setOptions({
position: 'top|right',
duration: 3000,
});
// -----------------------------------------------------------------------------
// PREVENT JQUERY CONFLICT
// -----------------------------------------------------------------------------
var $ = window.$;
var jQuery = window.jQuery;
this.$ = this.jQuery = jQuery.noConflict(true);
if (typeof $ == 'undefined') console.log('JQuery not found; The script will certainly fail');
// -----------------------------------------------------------------------------
// OPTIONS / CONFIG MENU
// -----------------------------------------------------------------------------
var parametros = {
code_box_expand : { type: 'checkbox', default: true },
code_box_height : { type: 'number', default: 900 },
code_font_resize : { type: 'checkbox', default: true },
code_font_size : { type: 'number', default: 12 },
page_wide : { type: 'checkbox', default: true },
};
var cfg;
try {
cfg = new MonkeyConfig({
title: 'Config DE_code_box',
menuCommand: true,
onSave: function() { fnSaveChanges(); },
params: parametros
});
console.log("MonkeyConfig loaded; The settings menu will be enabled");
}
catch(err) {
console.log(err);
console.log("MonkeyConfig not loaded; The settings menu will be disabled");
cfg = {
params: parametros,
get: function get(name) { return GM_getValue(name, this.params[name].default) }
}
}
// -----------------------------------------------------------------------------
// START
// -----------------------------------------------------------------------------
var shouldreload = false;
// apply imediately at document start
fnCheckChanges();
// also wait for page load. jquery will be ready here
$(function() {
// monitor the page for changes and reapply if necessary
// use 'observer.disconnect()' in 'fnCheckChanges()' to stop monitoring
var alvo = document.querySelector('body');
var observer = new MutationObserver(fnCheckChanges);
observer.observe(alvo, { attributes: true, characterData: true, childList: true, subtree: true });
});
// -----------------------------------------------------------------------------
// FUNCTIONS
// -----------------------------------------------------------------------------
function fnCheckChanges(changes, observer) {
var code_box_height = '';
if (cfg.get("code_box_expand")) code_box_height = cfg.get("code_box_height") + 'px';
$('code').css({'max-height':code_box_height});
var code_font_size = '';
if (cfg.get("code_font_resize")) code_font_size = cfg.get("code_font_size") + 'px';
$('blockquote, code, pre').css({'font-size':code_font_size});
var page_size = '';
var tl_margin = '';
var tb_float = '';
var tb_margin = '';
var tb_width = '';
if (cfg.get("page_wide")) {
page_size = 'unset';
tl_margin = '90%';
tb_float = 'unset';
tb_margin = '10%';
tb_width = 'unset';
}
$('#main-outlet').css({'max-width':page_size});
$('.timeline-container').css({'margin-left':tl_margin});
$('.topic-body').css({'float':tb_float, 'margin-right':tb_margin, 'width':tb_width});
}
// -----------------------------------------------------------------------------
function fnSaveChanges() {
$('body').on("click", "#reloadnow", function() {
$(this).fadeOut("fast", function() { document.location.reload(false); });
});
var msg_success = 'Settings saved';
toast.success(msg_success);
var msg_reload = '<span id="reloadnow"> Some changes will be applied after you reload the page. <br> Click here to reload now </span>';
if (shouldreload) toast.message(msg_reload, { delay: 3000, duration: 7000 });
}