-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstackoverflow-code-box-options.user.js
More file actions
191 lines (149 loc) · 7.15 KB
/
stackoverflow-code-box-options.user.js
File metadata and controls
191 lines (149 loc) · 7.15 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// ==UserScript==
// @name StackOverflow CodeBox Options
// @namespace http://stackoverflow.com/users/982924/rasg
// @author RASG
// @description Add options to StackOverflow 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*://*askubuntu.com/*
// @include http*://*mathoverflow.net/*
// @include http*://*serverfault.com/*
// @include http*://*stackapps.com/*
// @include http*://*stackexchange.com/*
// @include http*://*stackoverflow.com/*
// @include http*://*superuser.com/*
// @icon https://www.google.com/s2/favicons?domain=stackoverflow.com
// @version 2020.03.26.1002
// @grant GM_addStyle
// @grant GM_getMetadata
// @grant GM_getResourceText
// @grant GM_getValue
// @grant GM_notification
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_xmlhttpRequest
// ==/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 = {
answer_box_expand : { type: 'checkbox', default: true },
answer_box_height : { type: 'number', default: 400 },
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 },
open_links_in_new_tab : { type: 'checkbox', default: true },
page_wide : { type: 'checkbox', default: true },
remove_left_sidebar : { type: 'checkbox', default: false },
};
var cfg;
try {
cfg = new MonkeyConfig({
title: 'Config DF_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: false, characterData: false, childList: true, subtree: true });
});
// wait for SE specifics to be ready
var SEready = false;
StackExchange.ready (function() {
SEready = true;
console.log("StackExchange JS ready", SEready);
var meumenu = `
<ol class="nav-links">
<li class="youarehere"> <a id="navheader" href="#" class="pl8 js-gps-track nav-links--link -link__with-icon"> Scripted Options </a> </li>
<li> <a id="ec" href="#" class="js-gps-track nav-links--link"> Expand Comments </a> </li>
<li> <a id="wt" href="#" class="js-gps-track nav-links--link"> Watched Tags </a> </li>
<li> <a id="more" href="#" class="js-gps-track nav-links--link"> More ... </a> </li>
</ol>
`;
$(meumenu).insertAfter('#left-sidebar > div.left-sidebar--sticky-container.js-sticky-leftnav > nav > ol.nav-links');
$('#ec').click(function() { $('a.js-show-link').not(".dno").each(function() { StackExchange.comments.loadAll($(this)); }) })
$('#wt').click(function() { window.location = 'https://' + window.location.hostname + '/questions/tagged?tagMode=Watched'; })
$('#more').click(function() { cfg.open(); })
});
// -----------------------------------------------------------------------------
// FUNCTIONS
// -----------------------------------------------------------------------------
function fnCheckChanges(changes, observer) {
//if (SEready) a.b.c;
$('#mainbar').css({'text-align':'justify'});
(cfg.get("remove_left_sidebar")) ? $('.left-sidebar, #left-sidebar').hide() : $('.left-sidebar, #left-sidebar').show();
var answer_box_height = '';
if (cfg.get("answer_box_expand")) answer_box_height = cfg.get("answer_box_height") + 'px';
$('textarea').css({'height':answer_box_height});
var code_box_height = '';
if (cfg.get("code_box_expand")) code_box_height = cfg.get("code_box_height") + 'px';
$('pre').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 = '';
if (cfg.get("page_wide")) page_size = 'unset';
$('div.container, div#content').css({'max-width':page_size});
if (cfg.get("open_links_in_new_tab")) fnReplaceLinks();
}
// -----------------------------------------------------------------------------
function fnReplaceLinks() {
[...document.links].forEach(
a => $(a).filter(function() {
return this.href.match(/^http/) && this.hostname !== location.hostname;
}).attr('target', '_blank')
);
}
// -----------------------------------------------------------------------------
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 });
}