-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwider-comments.user.js
88 lines (80 loc) · 2.75 KB
/
wider-comments.user.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
// ==UserScript==
// @name Wider Comments
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to fix the very narrow comments section on medium
// @author You
// @match https://themakingofamillionaire.com/*
// @icon https://www.google.com/s2/favicons?domain=themakingofamillionaire.com
// @grant none
// ==/UserScript==
(function widerComments() {
const searchAllStylesheets = function searchAllStylesheets(searchTerm) {
const constructorName = searchTerm.constructor.name;
const rules = [];
const styleSheets = [...document.styleSheets];
styleSheets.forEach((styleSheet) => {
try {
const { cssRules } = styleSheet;
[...cssRules].forEach(({ cssText }) => {
if (
(constructorName === 'String' && cssText.includes(searchTerm))
|| (constructorName === 'RegExp' && cssText.match(searchTerm))
) {
rules.push(cssText);
}
});
} catch (e) {
console.error(e);
console.log('no css rules on ', styleSheet);
}
});
return rules;
};
console.log(searchAllStylesheets('414px'));
// '.lj { width: 414px; }';
const editRules = function editRules(rule, newRule) {
if (rule.constructor.name !== 'String' || newRule.constructor.name !== 'String') {
throw new Error('string terms only');
}
const styleSheets = [...document.styleSheets];
styleSheets.forEach((styleSheet, ssi) => {
try {
const cssRules = [...styleSheet.cssRules];
cssRules.forEach((cssRule, cri) => {
const { cssText } = cssRule;
if (cssText.includes(rule)) {
const editedText = cssText.replace(rule, newRule);
document.styleSheets[ssi].deleteRule(cri);
document.styleSheets[ssi].insertRule(editedText);
}
});
} catch (e) {
console.error(e);
console.log('could not access css rules on styleSheet, ', styleSheet);
}
});
};
const fixWidth = function fixWidth() {
const originalRule = '.lj { width: 414px; }';
// const transformedRule = '.aes { transform: translateX(-414px); }';
// const zxRule = '.zx { transform: translateX(-414px); }';
const rules = searchAllStylesheets(originalRule);
const translateRules = searchAllStylesheets('transform: translateX(-414px');
if (rules.length > 0 && translateRules.length > 0) {
console.log(rules);
console.log(translateRules);
editRules(rules[0], rules[0].replace('414px', '50vw'));
editRules(translateRules[0], translateRules[0].replace('414px', '50vw'));
document
.querySelectorAll('section.dt.gm.gn.do.go')
.forEach((e) => e.style.setProperty('width', '50vw'));
document.styleSheets[1].insertRule(
'.n.p > .av.aw.ax.ay.az.ba.bb.w { width: fit-content; left: 0; position: absolute; }',
);
} else {
setTimeout(fixWidth, 2000);
}
};
fixWidth();
}());