-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
131 lines (108 loc) · 3.53 KB
/
popup.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
const DEFAULT_FONT_SIZE = '12px';
const DEFAULT_LINE_HEIGHT = '19px';
const page = chrome.extension.getBackgroundPage();
let fontSizeDecrease = document.getElementById('font-size-decrease');
let fontSizeDisplay = document.getElementById('font-size-display');
let fontSizeIncrease = document.getElementById('font-size-increase');
let lineHeightDecrease = document.getElementById('line-height-decrease');
let lineHeightDisplay = document.getElementById('line-height-display');
let lineHeightIncrease = document.getElementById('line-height-increase');
let resetAll = document.getElementById('reset-all');
// init
chrome.storage.sync.get('fontSize', (data) => {
// set font in page from storage
changeFontSize(data.fontSize);
// init fontSizeDisplay
fontSizeDisplay.textContent = data.fontSize;
fontSizeDisplay.setAttribute('value', data.fontSize);
});
chrome.storage.sync.get('lineHeight', (data) => {
// set line height in page from storage
changeLineHeight(data.lineHeight);
// init lineHeightDisplay
lineHeightDisplay.textContent = data.lineHeight;
lineHeightDisplay.setAttribute('value', data.lineHeight);
})
function changeCSS(css) {
const STYLE_ELEMENT_ID = 'crucible-options-style';
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.insertCSS(
tabs[0].id,
{
code: css
}
)
});
}
/**
* Font size selector and attribute:
*
* .lineContent
* > font-size: 14px
*/
function changeFontSize(fontSize) {
const css = `
td.lineContent {
font-size: ${fontSize}px !important;
}
`;
changeCSS(css);
// update the display
fontSizeDisplay.textContent = fontSize;
fontSizeDisplay.setAttribute('value', fontSize);
}
/**
* Line height selector and attribute:
*
* .inlineSource tr.sourceLine
* > height: modifiable (default ~19px)
*/
function changeLineHeight(lineHeight) {
const css = `
tr.sourceLine {
height: ${lineHeight}px !important;
}
`;
changeCSS(css);
// update the display
lineHeightDisplay.textContent = lineHeight;
lineHeightDisplay.setAttribute('value', lineHeight);
}
function getCurrentFontSize() {
return parseInt(fontSizeDisplay.getAttribute('value').match(/[0-9]+/)[0]);
}
function getCurrentLineHeight() {
return parseInt(lineHeightDisplay.getAttribute('value').match(/[0-9]+/)[0]);
}
fontSizeDecrease.onclick = () => {
const currentFontSize = getCurrentFontSize();
const newFontSize = currentFontSize - 1;
changeFontSize(newFontSize);
chrome.storage.sync.set({fontSize: newFontSize});
}
fontSizeIncrease.onclick = () => {
const currentFontSize = getCurrentFontSize();
const newFontSize = currentFontSize + 1;
changeFontSize(newFontSize);
chrome.storage.sync.set({fontSize: newFontSize});
}
lineHeightDecrease.onclick = () => {
const currentLineHeight = getCurrentLineHeight();
const newLineHeight = currentLineHeight - 1;
changeLineHeight(newLineHeight);
chrome.storage.sync.set({lineHeight: newLineHeight});
}
lineHeightIncrease.onclick = () => {
const currentLineHeight = getCurrentLineHeight();
const newLineHeight = currentLineHeight + 1;
changeLineHeight(newLineHeight);
chrome.storage.sync.set({lineHeight: newLineHeight});
}
resetAll.onclick = () => {
const newFontSize = parseInt(DEFAULT_FONT_SIZE.match(/[0-9]+/)[0]);
const newLineHeight = parseInt(DEFAULT_LINE_HEIGHT.match(/[0-9]+/)[0]);
changeFontSize(newFontSize);
changeLineHeight(newLineHeight);
chrome.storage.sync.set({fontSize: DEFAULT_FONT_SIZE});
chrome.storage.sync.set({lineHeight: DEFAULT_LINE_HEIGHT});
}