-
Notifications
You must be signed in to change notification settings - Fork 6
/
contentscript.js
181 lines (154 loc) · 6.04 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// document.body.style.border = "15px solid red";
/*
TODO
- Fix and test the options UI
- Test and fix for Chrome, Brave & Firefox
*/
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
const fillTemplate = function(templateString, templateVars){
let safeTemplateVars = Object.assign({}, ...Object.keys(templateVars).map(k => ({[k]: escapeHtml(templateVars[k])})));
return new Function("return `"+templateString +"`;").call(safeTemplateVars);
}
const altTemplate =
"\
<a href='${this.url}' target='_blank'> \
<p style='margin: 0; font-size: 14px; font-weight: bold;'>${this.name} →</p> \
<p style='margin: 0; font-size: 12px;'>${this.desc}</p> \
</a>";
const CONTAINER_PADDING = "12px";
const CONTAINER_STYLES = `
.better-ext-container * {
all: unset;
}
.better-ext-container h1, .better-ext-container p, .better-ext-container div {
display:block;
}
.better-ext-container {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
font-size: 20px;
line-height:1;
background-color: #ffcc49;
position: fixed;
bottom: 10px;
right: 10px;
width: 300px;
box-shadow: 0px 10px 30px #222222;
padding: ${CONTAINER_PADDING};
text-align: center;
z-index: 20000;
}
.better-ext-container .better-ext-alt-container {
margin-left: -${CONTAINER_PADDING};
margin-right: -${CONTAINER_PADDING};
padding: 0 ${CONTAINER_PADDING};
margin-top: 4px;
max-height: 300px;
overflow: scroll;
background-color: #FEEBC8;
}
.better-ext-alt-container>a{
display: block;
border: 2px solid #222222;
border-radius: 4px;
margin-top: ${CONTAINER_PADDING};
color:#222222;
padding: 8px;
}
.better-ext-alt-container>a:last-child {
margin-bottom: ${CONTAINER_PADDING};
}
`;
function addPagePopupStyles() {
let styleTag = document.createElement("style");
styleTag.innerHTML = CONTAINER_STYLES;
document.head.append(styleTag);
}
function createNonRecommendedAlts(alternatives) {
let nonRecommendedAlts = document.createElement("div");
nonRecommendedAlts.setAttribute("style", "text-align: left;");
let alternativesHeading = document.createElement("p");
alternativesHeading.setAttribute("style", "font-size: 12px; text-align: center; margin: 4px 0;");
alternativesHeading.innerText = "MORE ALTERNATIVES";
nonRecommendedAlts.appendChild(alternativesHeading);
let scrollableAltContainer = document.createElement("div");
scrollableAltContainer.classList.add("better-ext-alt-container");
alternatives.map((alternative) => {
scrollableAltContainer.innerHTML += fillTemplate(altTemplate, alternative);
});
nonRecommendedAlts.appendChild(scrollableAltContainer);
return nonRecommendedAlts;
}
function createRecommendedAlt(recommendedAlternative) {
let recommendedAlt = document.createElement("div");
let betterBrandText = document.createElement("h1");
betterBrandText.textContent = "Better";
betterBrandText.setAttribute("style", "font-size: 32px; color: #222222; font-weight: bold; margin: 12px;");
let alternativeText = document.createElement("p");
alternativeText.textContent = recommendedAlternative.desc;
alternativeText.setAttribute(
"style",
"font-size: 20px; color: #222222; font-weight: bold; margin: 20px 0; line-height:1;"
);
let alternativeCTA = document.createElement("a");
alternativeCTA.textContent = recommendedAlternative.name;
alternativeCTA.innerHTML += " →";
alternativeCTA.setAttribute("href", escapeHtml(recommendedAlternative.url));
alternativeCTA.setAttribute("target", "_blank");
alternativeCTA.setAttribute("style", "display: inline-block; padding: 12px 24px; background-color: #222222; color: #ffffff; border-radius: 4px;");
recommendedAlt.appendChild(betterBrandText);
recommendedAlt.appendChild(alternativeText);
recommendedAlt.appendChild(alternativeCTA);
return recommendedAlt;
}
function showBetter(match) {
if(!match || !match.alternatives) return;
addPagePopupStyles();
let alternatives = match.alternatives;
let betterdiv = document.createElement("div");
betterdiv.classList.add("better-ext-container");
betterdiv.appendChild(createRecommendedAlt(alternatives[0]));
if(alternatives.length > 1) {
betterdiv.appendChild(createNonRecommendedAlts(alternatives.slice(1)));
}
let dismissButton = document.createElement("button");
dismissButton.innerHTML = "✗ Do not show suggestion for this URL";
dismissButton.setAttribute("style", "display: block; font-size: 14px; margin: 16px auto; cursor:pointer;");
dismissButton.addEventListener("click", () => {
document.body.removeChild(betterdiv);
dismissPermanently(match);
})
betterdiv.appendChild(dismissButton);
let closeButton = document.createElement("button");
closeButton.innerText = "❌";
closeButton.setAttribute("style", "position: absolute; top: -10px; right: -10px; cursor:pointer; background-color: white; border-radius: 15px; border: thin solid #aaa;");
closeButton.addEventListener("click", () => {
document.body.removeChild(betterdiv);
})
betterdiv.appendChild(closeButton);
document.body.appendChild(betterdiv);
}
function dismissPermanently(match) {
let key = `dismiss@${match.urlPattern}`
chrome.storage.local.set({
[key]: true,
});
}
function ifNotDissmissed(match, callback) {
let key = `dismiss@${match.urlPattern}`
chrome.storage.local.get(key, (item) => {
if (item && item[key]) {
return;
} else {
callback(match);
}
})
}
chrome.runtime.sendMessage({type: 'getMatch', url: document.location.href}, (response) => {
if (response) {
ifNotDissmissed(response, showBetter);
}
});