-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentScript.js
159 lines (140 loc) · 5.79 KB
/
contentScript.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
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
(() => {
"use strict";
let body = document.body;
let boardTitleSelector = "#boardTitle";
let toggleBodyLockSelector = "#toggle-body-lock";
let toolbarToolsSelector = "ul.xwd__toolbar--tools";
let settingsPanelSelector = "#settings-panel";
let pencilSelectors = [
".xwd__toolbar_icon--pencil",
".xwd__toolbar_icon--pencil-active"
];
let boardTitle = "Puzzle Board";
var methods = {
"init": () => {
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action === "toggle-pencil") {
document.querySelectorAll(pencilSelectors.join(","))[0].click();
}
else if (request.action === "toggle-body-lock") {
methods.toggleBodyLock();
}
}
);
// Create lock button
methods.waitForElement(toolbarToolsSelector, 0).then(function() {
methods.createLockButton();
methods.adjustSettingsPanel();
//console.log(document.getElementsByClassName("xwd__tool--button")[0]);
});
// Disable board title
methods.waitForElement("#xwd-board", 0).then(function() {
document.querySelector(boardTitleSelector).innerHTML = "";
});
},
"toggleBodyLock": () => {
var button = document.querySelector(toggleBodyLockSelector);
if (button.dataset.lockStatus == "unlocked"){
body.style.overflow = "hidden";
button.innerText = "Unlock";
button.style.width = "63px";
button.dataset.lockStatus = "locked";
button.style.background = "#4f85e5";
button.style.color = "#fff";
}
else {
body.style.overflow = "";
button.innerText = "Lock";
button.dataset.lockStatus = "unlocked";
button.style.width = "45px";
button.style.background = "#fff";
button.style.color = "#000";
}
},
"waitForElement": (querySelector, timeout) => {
return new Promise((resolve, reject)=>{
var timer = false;
if(document.querySelectorAll(querySelector).length) return resolve();
const observer = new MutationObserver(()=>{
if(document.querySelectorAll(querySelector).length){
observer.disconnect();
if(timer !== false) clearTimeout(timer);
return resolve();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
if(timeout) timer = setTimeout(()=>{
observer.disconnect();
reject();
}, timeout);
});
},
"createLockButton": () => {
var parentElement = document.querySelector("ul.xwd__toolbar--tools");
var div = document.createElement("div");
var li = document.createElement("li");
var button = document.createElement("button");
button.id = "toggle-body-lock";
button.type = "button";
button.ariaLabel = "Lock Body Scroll";
button.innerHTML = "Lock";
button.dataset.lockStatus = "unlocked";
li.className = "xwd__tool--button";
li.appendChild(button);
div.appendChild(li).onclick;
div.onclick = methods.toggleBodyLock;
parentElement.insertBefore(div, parentElement.children[1]);
},
"adjustSettingsPanel": () => {
document.getElementsByClassName("xwd__tool--button")[0].addEventListener("click", function() {
methods.waitForElement(settingsPanelSelector, 0).then(function() {
var settingsColumns = document.getElementsByClassName("xwd__settings-modal--column");
var settingsColumn0 = settingsColumns[0];
var settingsColumn1 = settingsColumns[1];
var settingsColumn1Length = settingsColumn1.children.length
for (var i = 0; i < settingsColumn1Length; i++) {
settingsColumn0.appendChild(settingsColumn1.children[0]);
}
settingsColumn0.className = "xwd__settings-modal";
settingsColumn1.remove();
// var section = document.createElement("section");
// var header = document.createElement("header");
// var insetDiv = document.createElement("div");
// var boardTitleSettingLabel = document.createElement("label");
// var boardTitleSettingInput = document.createElement("input");
// var boardTitleSettingSpan = document.createElement("span");
// section.className = "xwd__settings-modal--section";
// header.className = "xwd__settings-modal--heading";
// header.innerText = "NYT Crossword Plus Settings";
// insetDiv.className = "xwd__settings-modal--inset"
// boardTitleSettingInput.type = "checkbox";
// boardTitleSettingInput.setAttribute("name", "checkbox");
// boardTitleSettingInput.setAttribute("tabindex", "0");
// boardTitleSettingInput.setAttribute("readonly", true);
// boardTitleSettingInput.setAttribute("value", true);
// boardTitleSettingInput.setAttribute("checked", true);
// boardTitleSettingSpan.innerText = "Hide board tooltip"
// boardTitleSettingLabel.appendChild(boardTitleSettingInput);
// boardTitleSettingLabel.appendChild(boardTitleSettingSpan);
// insetDiv.appendChild(boardTitleSettingLabel);
// section.appendChild(header);
// section.appendChild(insetDiv);
// settingsColumn0.appendChild(section);
// boardTitleSettingInput.addEventListener("change", (event) => {
// if (event.currentTarget.checked) {
// document.querySelector(boardTitleSelector).innerHTML = "";
// }
// else {
// document.querySelector(boardTitleSelector).innerHTML = boardTitle;
// }
// });
});
});
}
}
methods.init();
})();