Skip to content

Commit f136a5d

Browse files
author
hoang.tran12
committed
popup - WIP
1 parent ea246f4 commit f136a5d

File tree

6 files changed

+130
-32
lines changed

6 files changed

+130
-32
lines changed

popup/index.js

Lines changed: 71 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import { checkPass } from "../scripts/auto_lockWebsite.js";
4040
// import _ from "../md/exportScriptsToMd.js";
4141

4242
const settingsBtn = document.querySelector(".settings");
43-
const reloadBtn = document.querySelector(".reload");
43+
const openInNewTabBtn = document.querySelector(".open-in-newtab");
4444
const tabDiv = document.querySelector("div.tab");
4545
const contentDiv = document.querySelector("div.content");
4646
const searchInput = document.querySelector(".search input");
@@ -502,30 +502,41 @@ function initTooltip() {
502502
"data-tooltip",
503503
t({ vi: "Cài đặt", en: "Settings" })
504504
);
505-
reloadBtn.setAttribute(
505+
openInNewTabBtn.setAttribute(
506506
"data-tooltip",
507-
t({ vi: "Khởi động lại tiện ích", en: "Reload extension" })
507+
t({ vi: "Mở trong tab mới", en: "Open in new tab" })
508508
);
509509
}
510510

511511
function initSettings() {
512-
reloadBtn.onclick = () => {
513-
Swal.fire({
514-
icon: "warning",
515-
title: t({
516-
vi: "Khởi động lại tiện ích?",
517-
en: "Reload extension?",
518-
}),
519-
text: t({
520-
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.",
521-
en: "Autorun scripts will be disconnected => you have to reload websites.",
522-
}),
523-
showCancelButton: true,
524-
confirmButtonText: t({ vi: "Khởi động lại", en: "Reload" }),
525-
cancelButtonText: t({ vi: "Huỷ", en: "Cancel" }),
526-
}).then((res) => {
527-
if (res.isConfirmed) chrome.runtime.reload();
512+
openInNewTabBtn.onclick = async () => {
513+
let exist = await chrome.tabs.query({
514+
url: location.href,
528515
});
516+
if (exist.length > 0) {
517+
chrome.windows.update(exist[0].windowId, { focused: true });
518+
window.close();
519+
return;
520+
}
521+
522+
trackEvent("CLICK_OPEN_IN_NEW_TAB");
523+
524+
let width = document.documentElement.clientWidth,
525+
height = document.documentElement.clientHeight,
526+
left = window.screenLeft,
527+
top = window.screenTop;
528+
529+
chrome.windows.create({
530+
url: location.href,
531+
type: "popup",
532+
height,
533+
width,
534+
left,
535+
top,
536+
setSelfAsOpener: true,
537+
});
538+
539+
window.close();
529540
};
530541

531542
settingsBtn.onclick = () => {
@@ -677,6 +688,18 @@ function initSettings() {
677688
const resetRow = document.createElement("div");
678689
resetRow.classList.add("row");
679690
resetRow.innerHTML = `
691+
<button
692+
data-flow="bottom"
693+
data-tooltip="${t({
694+
vi: "Khởi động lại tiện ích",
695+
en: "Reload extension",
696+
})}">
697+
<i class="fa-solid fa-arrows-rotate fa-lg"></i>
698+
${t({
699+
vi: "Khởi động lại",
700+
en: "Reload",
701+
})}
702+
</button>
680703
<button
681704
data-flow="bottom"
682705
data-tooltip="${t({
@@ -690,7 +713,10 @@ function initSettings() {
690713
})}
691714
</button>
692715
`;
693-
const resetBtn = resetRow.querySelector("button");
716+
const [reloadBtn, resetBtn] = Array.from(
717+
resetRow.querySelectorAll("button")
718+
);
719+
reloadBtn.onclick = reload;
694720
resetBtn.onclick = reset;
695721
body.appendChild(resetRow);
696722

@@ -704,6 +730,25 @@ function initSettings() {
704730
};
705731
}
706732

733+
function reload() {
734+
Swal.fire({
735+
icon: "warning",
736+
title: t({
737+
vi: "Khởi động lại tiện ích?",
738+
en: "Reload extension?",
739+
}),
740+
text: t({
741+
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.",
742+
en: "Autorun scripts will be disconnected => you have to reload websites.",
743+
}),
744+
showCancelButton: true,
745+
confirmButtonText: t({ vi: "Khởi động lại", en: "Reload" }),
746+
cancelButtonText: t({ vi: "Huỷ", en: "Cancel" }),
747+
}).then((res) => {
748+
if (res.isConfirmed) chrome.runtime.reload();
749+
});
750+
}
751+
707752
async function backup() {
708753
trackEvent("BACKUP");
709754
const data = {
@@ -906,5 +951,11 @@ window.addEventListener("scroll", onScrollEnd);
906951
initScrollToTop();
907952
createTabs().then(restoreScroll);
908953

954+
chrome.windows.onFocusChanged.addListener((windowId) => {
955+
setTimeout(async () => {
956+
let currentTab = await getCurrentTab();
957+
}, 200);
958+
});
959+
909960
checkForUpdate();
910961
})();

popup/popup.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ <h3>
5656
<i class="fa-solid fa-gear fa-lg"></i>
5757
</div>
5858

59-
<!-- reload -->
60-
<div class="reload" data-tooltip="Reload extension" data-flow="right">
61-
<i class="fa-solid fa-arrows-rotate fa-lg"></i>
59+
<!-- open-in-newtab -->
60+
<div class="open-in-newtab" data-tooltip="Open in new tab" data-flow="right">
61+
<i class="fa-solid fa-up-right-from-square fa-lg"></i>
6262
</div>
6363

6464
<!-- modal -->

popup/tabs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const tabs = [
3333
{
3434
...CATEGORY.search,
3535
scripts: [
36-
// s._test,
36+
s._test,
3737
// s._ufs_statistic,
3838
s.similarWeb,
3939
s.similarWeb_bypassLimit,
@@ -1051,6 +1051,7 @@ async function refreshSpecialTabs() {
10511051

10521052
// add auto runned scripts
10531053
let currentTab = await getCurrentTab();
1054+
if (!currentTab) return;
10541055
let bgCached = await chrome.runtime.sendMessage({
10551056
action: "ufs-runInBackground",
10561057
data: { fnPath: "getCache", params: ["badges"] },

popup/themes/default.less

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ option {
9797
}
9898

9999
.settings,
100-
.reload {
100+
.open-in-newtab {
101101
position: absolute !important;
102102
width: 40px;
103103
height: 40px;
@@ -115,7 +115,7 @@ option {
115115
}
116116
}
117117

118-
.reload {
118+
.open-in-newtab {
119119
left: 0px;
120120
}
121121

scripts/_test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default {
1313
},
1414

1515
popupScript: {
16-
onClick: async () => {
16+
_onClick: async () => {
1717
let currentTab = await getCurrentTab();
1818
chrome.tabs.update(currentTab.id, { url: "javascript:alert(1)" });
1919
},
@@ -169,6 +169,19 @@ export default {
169169
},
170170

171171
contentScript: {
172+
onClick: () => {
173+
alert("test");
174+
},
175+
onDocumentEnd: () => {
176+
if (location.hostname === "anonyviet.com") {
177+
let url = new URL(location.href);
178+
if (url.searchParams.has("url")) {
179+
let target = url.searchParams.get("url");
180+
window.open(target, "_self");
181+
}
182+
}
183+
},
184+
172185
// sync element position accross all tabs
173186
_onDocumentStart: (details) => {
174187
console.log(details);

scripts/helpers/utils.js

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { version } =
77

88
const CACHED = {
99
userID: null,
10+
lastWindowIds: [],
1011
};
1112

1213
export function checkBlackWhiteList(script, url = location?.href) {
@@ -253,12 +254,44 @@ export const getAllTabs = async () => {
253254
};
254255

255256
// Lấy ra tab hiện tại, trong window sử dung gần nhất
256-
export const getCurrentTab = async () => {
257-
let tabs = await chrome.tabs.query({
258-
currentWindow: true,
259-
active: true,
257+
if (typeof chrome?.windows?.onFocusChanged?.addListener === "function") {
258+
chrome.windows.onFocusChanged.addListener((windowId) => {
259+
if (windowId !== chrome.windows.WINDOW_ID_NONE) {
260+
CACHED.lastWindowIds.unshift(windowId);
261+
262+
// remove duplicate
263+
for (let i = 1; i < CACHED.lastWindowIds.length; i++) {
264+
if (windowId === CACHED.lastWindowIds[i]) {
265+
CACHED.lastWindowIds.splice(i, 1);
266+
break;
267+
}
268+
}
269+
}
260270
});
261-
return tabs[0];
271+
}
272+
273+
export const getCurrentTab = async () => {
274+
let windows = await chrome.windows.getAll({ populate: true });
275+
let lastFocusedWindow = windows
276+
// sort windows by lastFocused
277+
.sort(
278+
(a, b) =>
279+
CACHED.lastWindowIds.indexOf(a.id) - CACHED.lastWindowIds.indexOf(b.id)
280+
)
281+
// get windows that not popup extension
282+
.filter(
283+
(w) =>
284+
w.type !== "popup" &&
285+
w.tabs[0].url !== chrome.runtime.getURL("/popup/popup.html")
286+
)?.[0];
287+
288+
console.log("lastFocusedWindow", lastFocusedWindow);
289+
290+
if (!lastFocusedWindow) return null;
291+
292+
let tab = lastFocusedWindow.tabs.find((tab) => tab.active);
293+
console.log("tabs", tab);
294+
return tab;
262295
};
263296

264297
export const getCurrentTabId = async () => {

0 commit comments

Comments
 (0)