Skip to content

Commit aefb4ab

Browse files
committed
script buttons + fix some bugs
1 parent 2c5ac3c commit aefb4ab

15 files changed

+284
-167
lines changed

empty_script.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ export default {
1111
vi: "",
1212
img: "",
1313
},
14+
15+
// buttons that show beside favorite/view source buttons
16+
// show on hover script
17+
buttons: [
18+
{
19+
icon: '<i class="fa-regular fa-circle-question"></i>',
20+
title: {
21+
vi: "Đây là gì?",
22+
en: "What is this?",
23+
},
24+
onPress: () => {},
25+
},
26+
],
27+
28+
// easier way to add info button into moreButtons list
29+
// show as
1430
infoLink: "",
1531

1632
changeLogs: {

popup/index.js

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,11 @@ function createScriptButton(script, isFavorite = false) {
160160

161161
// button checker
162162
if (canAutoRun(script)) {
163+
const checkmarkContainer = document.createElement("div");
164+
checkmarkContainer.setAttribute("data-flow", "right");
165+
163166
const checkmark = document.createElement("button");
164-
checkmark.className = "checkmark tooltip";
167+
checkmark.className = "checkmark";
165168
checkmark.onclick = async (e) => {
166169
let oldVal = await isActiveScript(script.id);
167170
let newVal = !oldVal;
@@ -174,11 +177,12 @@ function createScriptButton(script, isFavorite = false) {
174177

175178
setActiveScript(script.id, newVal);
176179
trackEvent(script.id + (newVal ? "-ON" : "-OFF"));
177-
updateButtonChecker(script, buttonContainer, newVal);
180+
updateButtonChecker(script, checkmarkContainer, newVal);
178181
};
179182

180-
buttonContainer.appendChild(checkmark);
181-
updateButtonChecker(script, buttonContainer);
183+
checkmarkContainer.appendChild(checkmark);
184+
buttonContainer.appendChild(checkmarkContainer);
185+
updateButtonChecker(script, checkmarkContainer);
182186
}
183187

184188
// button
@@ -234,31 +238,12 @@ function createScriptButton(script, isFavorite = false) {
234238
button.appendChild(badgeContainer);
235239
}
236240

237-
// button icon
241+
// script icon
238242
if (script.icon && typeof script.icon === "string") {
239-
// image icon
240-
if (
241-
script.icon.startsWith("/") ||
242-
script.icon.startsWith("http://") ||
243-
script.icon.startsWith("https://") ||
244-
script.icon.startsWith("data:image")
245-
) {
246-
const icon = document.createElement("img");
247-
icon.classList.add("icon-img");
248-
icon.src = script.icon;
249-
button.appendChild(icon);
250-
}
251-
252-
// text/html icon
253-
else {
254-
const icon = document.createElement("span");
255-
icon.classList.add("icon-html");
256-
icon.innerHTML = script.icon;
257-
button.appendChild(icon);
258-
}
243+
button.appendChild(createIcon(script.icon));
259244
}
260245

261-
// button title
246+
// script title
262247
const title = document.createElement("span");
263248
title.classList.add("btn-title");
264249
title.innerHTML = t(script.name);
@@ -267,28 +252,30 @@ function createScriptButton(script, isFavorite = false) {
267252
const more = document.createElement("span");
268253
more.classList.add("more");
269254

270-
// what this? button
271-
if (script.infoLink) {
272-
const infoBtn = document.createElement("div");
273-
infoBtn.innerHTML = `<i class="fa-regular fa-circle-question"></i>`;
274-
infoBtn.className = "more-item";
275-
if (typeof script.infoLink === "string") {
276-
infoBtn.title = t({
277-
en: "View info/demo",
278-
vi: "Xem giới thiệu/demo",
279-
});
280-
infoBtn.setAttribute("data-tooltip", infoBtn.title);
281-
infoBtn.setAttribute("data-flow", "left");
282-
}
283-
infoBtn.onclick = (e) => {
255+
// script buttons
256+
let scriptBtns = script.buttons ?? [];
257+
if (typeof script.infoLink === "string")
258+
scriptBtns.unshift({
259+
icon: `<i class="fa-regular fa-circle-question"></i>`,
260+
name: { en: "Info", vi: "Thông tin" },
261+
onClick: () => window.open(script.infoLink),
262+
});
263+
scriptBtns.forEach((btnConfig) => {
264+
const btn = document.createElement("div");
265+
btn.appendChild(createIcon(btnConfig.icon));
266+
btn.classList.add("more-item");
267+
const title = t(btnConfig.name);
268+
btn.setAttribute("data-tooltip", title);
269+
btn.setAttribute("data-flow", "left");
270+
btn.onclick = (e) => {
271+
// prevent to trigger other script's onClick funcs
284272
e.stopPropagation();
285273
e.preventDefault();
286-
trackEvent(script.id + "-INFO");
287-
if (typeof script.infoLink === "string") window.open(script.infoLink);
288-
if (typeof script.infoLink === "function") script.infoLink();
274+
trackEvent(script.id + "-BUTTON-" + title);
275+
btnConfig.onClick();
289276
};
290-
more.appendChild(infoBtn);
291-
}
277+
more.appendChild(btn);
278+
});
292279

293280
// add to favorite button
294281
const addFavoriteBtn = document.createElement("div");
@@ -310,12 +297,14 @@ function createScriptButton(script, isFavorite = false) {
310297
// view source button
311298
const viewSourceBtn = document.createElement("div");
312299
viewSourceBtn.innerHTML = `<i class="fa-solid fa-code"></i>`;
313-
viewSourceBtn.title = t({
314-
en: "View script source",
315-
vi: "Xem mã nguồn",
316-
});
317300
viewSourceBtn.className = "more-item";
318-
viewSourceBtn.setAttribute("data-tooltip", viewSourceBtn.title);
301+
viewSourceBtn.setAttribute(
302+
"data-tooltip",
303+
t({
304+
en: "View script source",
305+
vi: "Xem mã nguồn",
306+
})
307+
);
319308
viewSourceBtn.setAttribute("data-flow", "left");
320309
viewSourceBtn.onclick = (e) => {
321310
e.stopPropagation();
@@ -331,15 +320,6 @@ function createScriptButton(script, isFavorite = false) {
331320
tooltip.classList.add("tooltiptext");
332321
tooltip.innerHTML = t(script.description);
333322

334-
// if (script.infoLink) {
335-
// tooltip.innerHTML +=
336-
// "<br/><br/>" +
337-
// t({
338-
// vi: "<b>Bấm ? để xem chi tiết<b/>",
339-
// en: "<b>Click ? for more details<b/>",
340-
// });
341-
// }
342-
343323
if (script.description?.img) {
344324
tooltip.innerHTML += `<img src="${script.description.img}" style="width:80vw" />`;
345325
}
@@ -358,39 +338,66 @@ function createScriptButton(script, isFavorite = false) {
358338
return buttonContainer;
359339
}
360340

341+
function createIcon(srcOrHtml) {
342+
// image icon
343+
if (
344+
srcOrHtml.startsWith("/") ||
345+
srcOrHtml.startsWith("http://") ||
346+
srcOrHtml.startsWith("https://") ||
347+
srcOrHtml.startsWith("data:image")
348+
) {
349+
const img = document.createElement("img");
350+
img.classList.add("icon-img");
351+
img.src = srcOrHtml;
352+
return img;
353+
}
354+
355+
// text/html icon: usually fontAwesome icon
356+
else {
357+
const span = document.createElement("span");
358+
span.classList.add("icon-html");
359+
span.innerHTML = srcOrHtml;
360+
return span;
361+
}
362+
}
363+
361364
function updateFavBtn(btn, isFavorite) {
362365
let i = btn.querySelector("i");
363366
i.classList.toggle("active", isFavorite);
364367
i.classList.toggle("fa-regular", !isFavorite);
365368
i.classList.toggle("fa-solid", isFavorite);
366-
btn.title = isFavorite
367-
? t({
368-
en: "Remove from favorite",
369-
vi: "Xoá khỏi yêu thích",
370-
})
371-
: t({
372-
en: "Add to farovite",
373-
vi: "Thêm vào yêu thích",
374-
});
375-
btn.setAttribute("data-tooltip", btn.title);
369+
btn.setAttribute(
370+
"data-tooltip",
371+
isFavorite
372+
? t({
373+
en: "Remove from favorite",
374+
vi: "Xoá khỏi yêu thích",
375+
})
376+
: t({
377+
en: "Add to farovite",
378+
vi: "Thêm vào yêu thích",
379+
})
380+
);
376381
}
377382

378-
async function updateButtonChecker(script, button, val) {
379-
let checkmark = button.querySelector(".checkmark");
383+
async function updateButtonChecker(script, checkmarkContainer, val) {
384+
let checkmark = checkmarkContainer.querySelector(".checkmark");
380385
if (!checkmark) return;
386+
let tooltip = "";
381387
if (val ?? (await isActiveScript(script.id))) {
382388
checkmark.classList.add("active");
383-
checkmark.title = t({
389+
tooltip = t({
384390
vi: "Tắt tự động chạy",
385391
en: "Turn off Autorun",
386392
});
387393
} else {
388394
checkmark.classList.remove("active");
389-
checkmark.title = t({
395+
tooltip = t({
390396
vi: "Bật tự động chạy",
391397
en: "Turn on Autorun",
392398
});
393399
}
400+
checkmarkContainer.setAttribute("data-tooltip", tooltip);
394401
}
395402

396403
async function runScript(script) {
@@ -473,8 +480,8 @@ function initSettings() {
473480
en: "Reload extension?",
474481
}),
475482
text: t({
476-
vi: "Các chức năng tự chạy sẽ lỗi => cần tải lại trang web.",
477-
en: "Autorun scripts will be turned off => you have to reload website.",
483+
vi: "Các chức năng tự chạy sẽ mất kết nối, gây lỗi => cần tải lại các trang web để hoạt động trở lại.",
484+
en: "Autorun scripts will be disconnected => you have to reload websites.",
478485
}),
479486
showCancelButton: true,
480487
confirmButtonText: t({ vi: "Khởi động lại", en: "Reload" }),

popup/popup.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ <h3>
5050
</div>
5151

5252
<!-- settings -->
53-
<div class="settings" title="Settings" data-tooltip="Settings" data-flow="left">
53+
<div class="settings" data-tooltip="Settings" data-flow="left">
5454
<i class="fa-solid fa-gear fa-lg"></i>
5555
</div>
5656

5757
<!-- reload -->
58-
<div class="reload" title="Reload extension" data-tooltip="Reload extension" data-flow="right">
58+
<div class="reload" data-tooltip="Reload extension" data-flow="right">
5959
<i class="fa-solid fa-arrows-rotate fa-lg"></i>
6060
</div>
6161

@@ -75,7 +75,7 @@ <h3 class="title">Modal Title</h3>
7575
</div>
7676

7777
<!-- scroll to top -->
78-
<div id="scroll-to-top" class="hide" title="Scroll to top">
78+
<div id="scroll-to-top" class="hide">
7979
<i class="fa-solid fa-arrow-up fa-lg"></i>
8080
</div>
8181

0 commit comments

Comments
 (0)