Skip to content

Commit fc47623

Browse files
author
hoang.tran12
committed
optimize all imports
1 parent aefb4ab commit fc47623

Some content is hidden

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

57 files changed

+1756
-1738
lines changed

empty_script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// WARNING: avoid import anything here, use dynamic imports instead
1+
// WARNING: only import js inside scripts/ folder
22

33
export default {
44
icon: "",

popup/index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {
55
removeAccents,
66
setActiveScript,
77
trackEvent,
8-
debounce,
98
Storage,
109
checkWillRun,
1110
runScriptInTabWithEventChain,
1211
} from "../scripts/helpers/utils.js";
12+
import { UfsGlobal } from "../scripts/content-scripts/ufs_global.js";
1313
import { checkForUpdate } from "./helpers/checkForUpdate.js";
1414
import {
1515
LANG,
@@ -277,23 +277,6 @@ function createScriptButton(script, isFavorite = false) {
277277
more.appendChild(btn);
278278
});
279279

280-
// add to favorite button
281-
const addFavoriteBtn = document.createElement("div");
282-
addFavoriteBtn.innerHTML = `<i class="fa-solid fa-star"></i>`;
283-
addFavoriteBtn.className = "more-item";
284-
addFavoriteBtn.setAttribute("data-flow", "left");
285-
updateFavBtn(addFavoriteBtn, isFavorite);
286-
addFavoriteBtn.onclick = (e) => {
287-
e.stopPropagation();
288-
e.preventDefault();
289-
trackEvent(script.id + (isFavorite ? "-REMOVE-FAVORITE" : "-ADD-FAVORITE"));
290-
favoriteScriptsSaver.toggle(script);
291-
isFavorite = !isFavorite;
292-
updateFavBtn(addFavoriteBtn, isFavorite);
293-
refreshSpecialTabs();
294-
};
295-
more.appendChild(addFavoriteBtn);
296-
297280
// view source button
298281
const viewSourceBtn = document.createElement("div");
299282
viewSourceBtn.innerHTML = `<i class="fa-solid fa-code"></i>`;
@@ -315,6 +298,23 @@ function createScriptButton(script, isFavorite = false) {
315298
};
316299
more.appendChild(viewSourceBtn);
317300

301+
// add to favorite button
302+
const addFavoriteBtn = document.createElement("div");
303+
addFavoriteBtn.innerHTML = `<i class="fa-solid fa-star"></i>`;
304+
addFavoriteBtn.className = "more-item";
305+
addFavoriteBtn.setAttribute("data-flow", "left");
306+
updateFavBtn(addFavoriteBtn, isFavorite);
307+
addFavoriteBtn.onclick = (e) => {
308+
e.stopPropagation();
309+
e.preventDefault();
310+
trackEvent(script.id + (isFavorite ? "-REMOVE-FAVORITE" : "-ADD-FAVORITE"));
311+
favoriteScriptsSaver.toggle(script);
312+
isFavorite = !isFavorite;
313+
updateFavBtn(addFavoriteBtn, isFavorite);
314+
refreshSpecialTabs();
315+
};
316+
more.appendChild(addFavoriteBtn);
317+
318318
// tooltip
319319
const tooltip = document.createElement("span");
320320
tooltip.classList.add("tooltiptext");
@@ -363,9 +363,9 @@ function createIcon(srcOrHtml) {
363363

364364
function updateFavBtn(btn, isFavorite) {
365365
let i = btn.querySelector("i");
366-
i.classList.toggle("active", isFavorite);
367366
i.classList.toggle("fa-regular", !isFavorite);
368367
i.classList.toggle("fa-solid", isFavorite);
368+
btn.classList.toggle("active", isFavorite);
369369
btn.setAttribute(
370370
"data-tooltip",
371371
isFavorite
@@ -647,7 +647,7 @@ function restoreScroll() {
647647
});
648648
}
649649

650-
const onScrollEnd = debounce(() => {
650+
const onScrollEnd = UfsGlobal.Utils.debounce(() => {
651651
saveScroll();
652652
scrollToTopBtn.classList.toggle("hide", document.body.scrollTop < 200);
653653
}, 100);

popup/main.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
import("./index.js").then(() => {
22
document.querySelector("#loading-fullscreen")?.remove();
33
});
4-
5-
import("../scripts/content-scripts/ufs_global.js", (success, fail) => {
6-
if (success) console.log("UfsGlobal injected");
7-
else console.log("UfsGlobal injection failed: ", fail);
8-
});

popup/styles/default.less

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -316,41 +316,39 @@ option {
316316
z-index: 3;
317317
border: linear-gradient(rgb(210 213 219) 1px, transparent 1px), linear-gradient(to right, rgb(210 213 219) 1px, rgb(240 241 245) 1px);
318318

319-
.more .more-item {
320-
margin-left: 5px;
319+
.more-item {
321320
opacity: 1;
322-
323-
i {
324-
width: auto;
325-
}
326321
}
327322
}
328323

329-
.more {
330-
.more-item {
331-
display: inline-block;
324+
.more-item {
325+
display: inline-block;
326+
transition: all .2s ease;
327+
margin-left: 5px;
328+
opacity: 0;
329+
330+
i {
331+
transition: all .2s ease;
332+
}
332333

334+
&.active {
333335
i {
334-
transition: all .2s;
336+
color: @color_highlight;
335337
}
336338

337-
&:hover i {
338-
transform: scale(1.5);
339-
}
339+
opacity: 1;
340+
}
340341

341-
&[data-tooltip]:hover {
342-
transform: scale(1.1);
343-
}
342+
&:hover i {
343+
transform: scale(1.5);
344+
}
344345

345-
& i.active {
346-
color: @color_highlight;
347-
width: auto;
348-
margin-left: 5px;
349-
opacity: 1;
350-
}
346+
&[data-tooltip]:hover {
347+
transform: scale(1.1);
351348
}
352349
}
353350

351+
354352
.btn-title {
355353
vertical-align: middle;
356354
flex: 1;

scripts/_test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ export default {
1010
},
1111

1212
popupScript: {
13-
onClick: () => {
14-
chrome.sessions.getRecentlyClosed({}, (data) => {
15-
console.log(data);
16-
});
13+
onClick: async () => {
14+
const { UfsGlobal } = await import("./content-scripts/ufs_global.js");
15+
console.log(UfsGlobal);
1716
},
1817

1918
// fake window update screen

scripts/_ufs_statistic.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UfsGlobal } from "./content-scripts/ufs_global.js";
2+
13
export default {
24
icon: "/assets/icon32.png",
35
name: {

scripts/auto_redirectLargestImageSrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UfsGlobal } from "./content-scripts/ufs_global.js";
2+
13
export default {
24
icon: '<i class="fa-solid fa-up-right-and-down-left-from-center"></i>',
35
name: {

scripts/background-scripts/background_script.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import * as utils from "../helpers/utils.js";
22
import { allScripts } from "../index.js";
3-
import "../content-scripts/ufs_global.js"; // https://stackoverflow.com/a/62806068/23648002
4-
5-
console.log(UfsGlobal);
3+
import { UfsGlobal } from "../content-scripts/ufs_global.js";
4+
// import "../content-scripts/ufs_global.js"; // https://stackoverflow.com/a/62806068/23648002
65

76
const {
87
convertBlobToBase64,
@@ -14,6 +13,7 @@ const {
1413

1514
const { ISOLATED, MAIN } = chrome.scripting.ExecutionWorld;
1615
const CACHED = {
16+
path: chrome.runtime.getURL("/scripts/"),
1717
activeScriptIds: [],
1818
badges: {},
1919
};
@@ -317,12 +317,13 @@ function listenNavigation() {
317317
try {
318318
const { tabId, frameId } = getDetailIds(details);
319319

320-
// inject ufsglobal, contentscript, pagescript before run any scripts
321320
if (eventChain === "onDocumentStart") {
322321
// clear badge cache on main frame
323322
if (details.frameId === 0) CACHED.badges[tabId] = [];
323+
// inject ufs global
324324
injectUfsGlobal(tabId, frameId);
325325
}
326+
326327
runScriptsTab(eventChain, MAIN, details);
327328
runScriptsTab(eventChain, ISOLATED, details);
328329
runScriptsBackground(eventChain, details);
@@ -393,19 +394,26 @@ function injectUfsGlobal(tabId, frameId) {
393394
{ files: ["ufs_global.js", "content_script.js"], world: ISOLATED },
394395
{ files: ["ufs_global.js"], world: MAIN },
395396
].forEach(({ files, world }) => {
397+
let paths = files.map((file) => CACHED.path + "content-scripts/" + file);
396398
utils.runScriptFile({
397-
files: files.map((file) => "/scripts/content-scripts/" + file),
398399
target: {
399400
tabId: tabId,
400401
frameIds: [frameId],
401402
},
403+
func: (paths, frameId) => {
404+
paths.forEach((path) => {
405+
import(path)
406+
.then(() => console.log("Ufs import SUCCESS", frameId, path))
407+
.catch((e) => console.error("Ufs import FAILED", frameId, e));
408+
});
409+
},
410+
args: [paths, frameId],
402411
world: world,
403412
});
404413
});
405414
}
406415

407416
function main() {
408-
// listen change active scripts
409417
cacheActiveScriptIds();
410418
chrome.storage.onChanged.addListener((changes, areaName) => {
411419
// areaName = "local" / "sync" / "managed" / "session" ...
@@ -437,17 +445,6 @@ function main() {
437445
trackEvent("ufs-INSTALLED");
438446

439447
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-
});
451448
});
452449

453450
chrome.action.setBadgeBackgroundColor({ color: "#666" });
@@ -459,5 +456,3 @@ try {
459456
} catch (e) {
460457
console.log("ERROR:", e);
461458
}
462-
463-
// https://developer.chrome.com/docs/extensions/develop/migrate/blocking-web-requests

scripts/bookmark_exporter.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UfsGlobal } from "./content-scripts/ufs_global.js";
2+
13
export default {
24
icon: '<i class="fa-solid fa-bookmark fa-lg"></i>',
35
name: {
@@ -14,7 +16,7 @@ export default {
1416
},
1517

1618
popupScript: {
17-
onClick: function () {
19+
onClick: async function () {
1820
chrome.bookmarks.getTree((tree) => {
1921
console.log(tree);
2022

0 commit comments

Comments
 (0)