-
Notifications
You must be signed in to change notification settings - Fork 6
/
content-scripts.js
98 lines (82 loc) · 3.01 KB
/
content-scripts.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
/**
* Adds a button to the toolbar and when activated, applies .birdseye class to body
*/
// Initializes on first page load +
// uggggllly hax to continiously check whether user has switched page/board
document.onreadystatechange = function () {
if (document.readyState === 'complete') {
shouldInit()
setInterval(shouldInit, 3000)
}
}
// Checks whether the button element is present - if not (user switched page/board), then init
function shouldInit(event) {
let buttonTarget = document.getElementsByClassName('board-header-btns mod-right')
let buttonContainer = document.getElementsByClassName('birdseye-button')
if (buttonTarget.length !== 0 && buttonContainer.length === 0) {
initApplication()
}
}
// Create the button and inject into to toolbar
function initApplication() {
let buttonContainer = document.createElement('a')
buttonContainer.className = 'birdseye-button board-header-btn'
buttonContainer.setAttribute('href', '#')
buttonContainer.setAttribute('id', 'BirdseyeToggle')
buttonContainer.addEventListener('click', clickButton)
let iconContainer = document.createElement('span')
iconContainer.className = 'icon-sm icon-subscribe board-header-btn-icon'
let textContainer = document.createElement('span')
textContainer.className = 'board-header-btn-text u-text-underline'
let text = document.createTextNode('Birds Eye')
textContainer.appendChild(text)
buttonContainer.appendChild(iconContainer)
buttonContainer.appendChild(textContainer)
let buttonTarget = document.getElementsByClassName('board-header-btns mod-right')[0]
buttonTarget.insertBefore(buttonContainer, buttonTarget.firstChild)
// When switching pages/boards, the button loses its enabled class, so re-add it
let body = document.getElementsByTagName('body')[0]
if (body.classList.contains('birdseye')) {
buttonContainer.classList.toggle('board-header-btn-enabled')
}
BirdseyeLoadState()
}
// Clicking on the button triggers the class toggling
function clickButton() {
let button = document.getElementById('BirdseyeToggle')
setState(!(button.dataset.state == 'true' ? true : false))
}
function setState(state) {
let button = document.getElementById('BirdseyeToggle')
let body = document.getElementsByTagName('body')[0]
if (state == true) {
body.classList.add('birdseye')
button.classList.add('board-header-btn-enabled')
} else {
body.classList.remove('birdseye')
button.classList.remove('board-header-btn-enabled')
}
button.dataset.state = state
BirdseyeSaveState(state)
}
function BirdseyeSaveState(state) {
chrome.storage.sync.set({
'BirdseyeState': state
}, () => {})
}
function BirdseyeLoadState() {
//Get the button state from chrome storage, if its different from what is set, set it to new state
try {
chrome.storage.sync.get('BirdseyeState', function (items) {
if (items.BirdseyeState == true) {
setState(true)
} else {
setState(false)
}
})
} catch (e) {
chrome.storage.sync.set({
'BirdseyeState': false
})
}
}