Skip to content

Commit 8895e97

Browse files
author
hoang.tran12
committed
a lot of optimize + refactor + split ufsGlobal (WIP)
1 parent ac9ac89 commit 8895e97

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1290
-1586
lines changed

empty_script.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,3 @@ export default {
156156
},
157157
},
158158
};
159-
160-
// functions/attributes that other scripts can import and use
161-
// can only used by popupScript, backgroundScript
162-
export const shared = {};

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"history",
2424
"storage",
2525
"cookies",
26+
"sessions",
2627
"debugger",
2728
"activeTab",
2829
"downloads",

popup/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { refreshSpecialTabs, getAllTabs } from "./tabs.js";
3636
// import _ from "../md/exportScriptsToMd.js";
3737

3838
const settingsBtn = document.querySelector(".settings");
39+
const reloadBtn = document.querySelector(".reload");
3940
const tabDiv = document.querySelector("div.tab");
4041
const contentDiv = document.querySelector("div.content");
4142
const searchInput = document.querySelector(".search input");
@@ -441,7 +442,22 @@ async function runScript(script) {
441442
// ========================================================
442443
// #region others
443444

445+
function initTooltip() {
446+
settingsBtn.setAttribute(
447+
"data-tooltip",
448+
t({ vi: "Cài đặt", en: "Settings" })
449+
);
450+
reloadBtn.setAttribute(
451+
"data-tooltip",
452+
t({ vi: "Khởi động lại tiện ích", en: "Reload extension" })
453+
);
454+
}
455+
444456
function initSettings() {
457+
reloadBtn.onclick = () => {
458+
chrome.runtime.reload();
459+
};
460+
445461
settingsBtn.onclick = () => {
446462
trackEvent("CLICK_SETTINGS");
447463

@@ -479,6 +495,7 @@ function initSettings() {
479495

480496
// re-open setting modal
481497
settingsBtn.click();
498+
initTooltip();
482499
};
483500
body.appendChild(langRow);
484501

@@ -518,6 +535,7 @@ function initSettings() {
518535

519536
// re-open setting modal
520537
settingsBtn.click();
538+
initTooltip();
521539
};
522540
body.appendChild(themeRow);
523541

@@ -610,6 +628,7 @@ window.addEventListener("scroll", onScrollEnd);
610628

611629
initTracking();
612630
initSearch();
631+
initTooltip();
613632
initSettings();
614633
initScrollToTop();
615634
createTabs().then(restoreScroll);

popup/popup.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ <h3>
5454
<i class="fa-solid fa-gear fa-lg"></i>
5555
</div>
5656

57+
<!-- reload -->
58+
<div class="reload" title="Reload extension" data-tooltip="Reload extension" data-flow="right">
59+
<i class="fa-solid fa-arrows-rotate fa-lg"></i>
60+
</div>
61+
5762
<!-- modal -->
5863
<div id="myModal" class="modal hide">
5964
<!-- Modal content -->

popup/styles/default.less

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ option {
9696
color: @color_body;
9797
}
9898

99-
.settings {
99+
.settings,
100+
.reload {
100101
position: absolute;
101102
width: 40px;
102103
height: 40px;
@@ -114,6 +115,10 @@ option {
114115
}
115116
}
116117

118+
.reload {
119+
left: 0px;
120+
}
121+
117122
.settings-body {
118123
.row {
119124
display: flex;

popup/tabs.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,6 @@ const tabs = [
815815
s.viewScriptsUsed,
816816
s.viewStylesUsed,
817817
s.cssSelectorViewer,
818-
s.viewPartialSource,
819818
createTitle("--- Remove ---", "--- Xoá ---"),
820819
s.removeColours,
821820
s.removeStylesheet,

scripts/_test.js

Lines changed: 14 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ export default {
1010
},
1111

1212
popupScript: {
13+
onClick: () => {
14+
chrome.sessions.getRecentlyClosed({}, (data) => {
15+
console.log(data);
16+
});
17+
},
18+
19+
// fake window update screen
1320
_onClick: async () => {
1421
const { openWebAndRunScript } = await import("./helpers/utils.js");
1522
openWebAndRunScript({
@@ -23,6 +30,8 @@ export default {
2330
waitUntilLoadEnd: true,
2431
});
2532
},
33+
34+
// saveAsMHTML
2635
_onClick: async () => {
2736
const { getCurrentTab, showLoading } = await import("./helpers/utils.js");
2837
const tab = await getCurrentTab();
@@ -36,6 +45,8 @@ export default {
3645
filename: "web.mhtml",
3746
});
3847
},
48+
49+
// Delete browsers history
3950
_onClick: async () => {
4051
const { getCurrentTab, showLoading } = await import("./helpers/utils.js");
4152

@@ -97,6 +108,8 @@ export default {
97108
]
98109
*/
99110
},
111+
112+
// devtool command
100113
_onClick: async () => {
101114
// const {
102115
// attachDebugger,
@@ -150,6 +163,7 @@ export default {
150163
},
151164

152165
contentScript: {
166+
// text size in KB
153167
_onClick: () => {
154168
function formatSize(size, fixed = 0) {
155169
size = Number(size);
@@ -187,55 +201,7 @@ export default {
187201
alert(e);
188202
}
189203
},
190-
_onClick: () => {
191-
function analyzeWebpage() {
192-
const href = window.location.href,
193-
hostname = window.location.hostname,
194-
a = href.replace("www.", "");
195-
196-
const features = {
197-
"IP Address": (() => {
198-
const ipv4Regex =
199-
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
200-
const ipv6Regex = /^0x([0-9a-fA-F]{2})(.|$){3}[0-9a-fA-F]{2}$/;
201-
if (ipv4Regex.test(hostname) || ipv6Regex.test(hostname)) return 1;
202-
return -1;
203-
})(),
204-
"URL Length":
205-
href.length < 54
206-
? -1
207-
: href.length >= 54 && href.length <= 75
208-
? 0
209-
: 1,
210-
"Tiny URL": a.length < 7 ? 1 : -1,
211-
"@ Symbol": /@/.test(href) ? 1 : -1,
212-
"Redirecting using //": href.lastIndexOf("//") > 7 ? 1 : -1,
213-
"(-) Prefix/Suffix in domain": /-/.test(hostname) ? 1 : -1,
214-
"No. of Sub Domains": (() => {
215-
let len = (a.match(RegExp("\\.", "g")) || []).length;
216-
return len == 1 ? -1 : len == 2 ? 0 : 1;
217-
})(),
218-
HTTPS: /https:\/\//.test(href) ? -1 : 1,
219-
Favicon: (() => {
220-
let icon;
221-
const c = document.getElementsByTagName("link");
222-
for (let t = 0; t < c.length; t++) {
223-
("icon" != c[t].getAttribute("rel") &&
224-
"shortcut icon" != c[t].getAttribute("rel")) ||
225-
(icon = c[t].getAttribute("href"));
226-
}
227-
if (!icon || icon.length != 12) return -1;
228-
229-
let i = RegExp(hostname, "g");
230-
return i.test(icon) ? -1 : 1;
231-
})(),
232-
};
233204

234-
console.log("Webpage Features:", features);
235-
}
236-
237-
analyzeWebpage();
238-
},
239205
// render video in document.title
240206
_onClick: () => {
241207
let video = document.querySelector("video");
@@ -280,17 +246,6 @@ export default {
280246
updateFavicon();
281247
},
282248
},
283-
284-
pageScript: {
285-
_onClick: async () => {
286-
console.log("send");
287-
let res = await UfsGlobal.Extension.runInContentScript(
288-
"chrome.runtime.sendMessage",
289-
[{ action: "test" }, "callback"]
290-
);
291-
console.log(res);
292-
},
293-
},
294249
};
295250

296251
const backup = () => {

scripts/background-scripts/background_script.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -321,19 +321,7 @@ function listenNavigation() {
321321
if (eventChain === "onDocumentStart") {
322322
// clear badge cache on main frame
323323
if (details.frameId === 0) CACHED.badges[tabId] = [];
324-
[
325-
{ files: ["ufs_global.js", "content_script.js"], world: ISOLATED },
326-
{ files: ["ufs_global.js"], world: MAIN },
327-
].forEach(({ files, world }) => {
328-
utils.runScriptFile({
329-
files: files.map((file) => "/scripts/content-scripts/" + file),
330-
target: {
331-
tabId: tabId,
332-
frameIds: [frameId],
333-
},
334-
world: world,
335-
});
336-
});
324+
injectUfsGlobal(tabId, frameId);
337325
}
338326
runScriptsTab(eventChain, MAIN, details);
339327
runScriptsTab(eventChain, ISOLATED, details);
@@ -400,6 +388,22 @@ function listenMessage() {
400388
});
401389
}
402390

391+
function injectUfsGlobal(tabId, frameId) {
392+
[
393+
{ files: ["ufs_global.js", "content_script.js"], world: ISOLATED },
394+
{ files: ["ufs_global.js"], world: MAIN },
395+
].forEach(({ files, world }) => {
396+
utils.runScriptFile({
397+
files: files.map((file) => "/scripts/content-scripts/" + file),
398+
target: {
399+
tabId: tabId,
400+
frameIds: [frameId],
401+
},
402+
world: world,
403+
});
404+
});
405+
}
406+
403407
function main() {
404408
// listen change active scripts
405409
cacheActiveScriptIds();
@@ -433,6 +437,17 @@ function main() {
433437
trackEvent("ufs-INSTALLED");
434438

435439
runScriptsBackground("runtime.onInstalled", null, reason, true);
440+
441+
// inject ufsGlobal to all frames in all tabs
442+
chrome.tabs.query({}, (tabs) => {
443+
tabs.forEach((tab) => {
444+
chrome.webNavigation.getAllFrames({ tabId: tab.id }, (frames) => {
445+
frames.forEach((frame) => {
446+
injectUfsGlobal(tab.id, frame.frameId);
447+
});
448+
});
449+
});
450+
});
436451
});
437452

438453
chrome.action.setBadgeBackgroundColor({ color: "#666" });

scripts/changeAudioOutput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
infoLink:
1212
"https://www.facebook.com/groups/j2team.community/posts/1362716140727169/",
1313

14-
contentScript: {
14+
pageScript: {
1515
// Source: https://gist.github.com/monokaijs/44ef4bd0770f83272b83c038a2769c90
1616
onClick: () => {
1717
(async function () {

0 commit comments

Comments
 (0)