Skip to content

Commit fafa5de

Browse files
author
hoang.tran12
committed
optimize magnify image + not require for enable to use
1 parent cdec722 commit fafa5de

File tree

5 files changed

+895
-908
lines changed

5 files changed

+895
-908
lines changed

popup/helpers/utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const canClick = (script) => {
1414

1515
function hasChildFunction(object, excludedNamesSet = new Set()) {
1616
for (let key in object) {
17+
if (key.startsWith("_")) continue;
1718
if (!object[key]) continue;
1819
if (excludedNamesSet.has(key)) continue;
1920
if (typeof object[key] === "function") return true;

scripts/_test.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ export default {
150150
},
151151

152152
contentScript: {
153-
onClick: () => {
153+
onClick() {
154+
console.log(UfsGlobal.DOM.getMousePos());
155+
},
156+
_onClick: () => {
154157
function formatSize(size, fixed = 0) {
155158
size = Number(size);
156159
if (!size) return "?";
@@ -289,29 +292,6 @@ export default {
289292
console.log(res);
290293
},
291294
},
292-
293-
backgroundScript: {
294-
runtime: {
295-
onInstalled: () => {
296-
console.log("installed");
297-
},
298-
onStartup: () => {
299-
console.log("startup");
300-
},
301-
onMessage: ({ request, sender, sendResponse }) => {
302-
console.log(request, sender, sendResponse);
303-
if (request.action === "test") {
304-
sendResponse({ data: "result test" });
305-
}
306-
return null;
307-
},
308-
},
309-
storage: {
310-
onChanged: (details) => {
311-
console.log("on Storage changed", details);
312-
},
313-
},
314-
},
315295
};
316296

317297
const backup = () => {

scripts/background-scripts/background_script.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ function main() {
351351
listenTabs();
352352
listenMessage();
353353

354-
chrome.contextMenus.onClicked.addListener((info) => {
354+
chrome.contextMenus.onClicked.addListener(async (info) => {
355355
console.log(info);
356356
if (info.menuItemId == "ufs-magnify-image") {
357357
trackEvent("magnify-image-CONTEXT-MENU");
@@ -366,21 +366,17 @@ function main() {
366366
"pageUrl": "https://www.deviantart.com/kat-zaphire/art/Deep-in-the-forest-989494503",
367367
"srcUrl": "https://wixmp-70a14ff54af6225c7974eec7.wixmp.com/offers-assets/94f22a36-bb47-4836-8bce-fea45f844aa4.gif"
368368
} */
369-
runScriptInCurrentTab(
370-
(imgUrl) => {
371-
let fn = window.top?.ufs_magnify_image_createPreview;
372-
if (typeof fn === "function") {
373-
fn(imgUrl, window.top.innerWidth / 2, window.top.innerHeight / 2);
374-
} else {
375-
alert(
376-
"Useful-script:\n\n" +
377-
"Vui lòng bật chức năng 'Tự động hoá' > 'Phóng to mọi hình ảnh' trước.\n\n" +
378-
"Please enable 'Automation' > 'Magnify any Image' first."
379-
);
380-
}
369+
let tab = await utils.getCurrentTab();
370+
utils.runScriptInTabWithEventChain({
371+
target: {
372+
tabId: tab.id,
373+
frameIds: [0],
381374
},
382-
[info.srcUrl]
383-
);
375+
scriptIds: ["magnify_image"],
376+
eventChain: "contentScript._createPreview",
377+
details: info,
378+
world: "ISOLATED",
379+
});
384380
}
385381
});
386382

scripts/content-scripts/ufs_global.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Call UseGlobal directly, no need to import
1616
waitForTabToLoad,
1717
},
1818
DOM: {
19+
getMousePos,
1920
isInCrossOriginFrame,
2021
isInIframe,
2122
checkElementvisibility,
@@ -111,7 +112,24 @@ Call UseGlobal directly, no need to import
111112
});
112113

113114
// store cache for all functions in UfsGlobal
114-
const CACHED = {};
115+
const CACHED = {
116+
mouse: {
117+
x: 0,
118+
y: 0,
119+
},
120+
};
121+
122+
if (typeof window !== "undefined") {
123+
if (typeof window?.addEventListener === "function") {
124+
window.addEventListener("mousemove", (e) => {
125+
CACHED.mouse.x = e.clientX;
126+
CACHED.mouse.y = e.clientY;
127+
});
128+
}
129+
}
130+
function getMousePos() {
131+
return CACHED.mouse;
132+
}
115133

116134
// #region Extension
117135

@@ -664,11 +682,12 @@ Call UseGlobal directly, no need to import
664682
else css.innerText = code;
665683
(document.head || document.documentElement).appendChild(css);
666684
}
667-
function injectCssFile(filePath) {
685+
function injectCssFile(filePath, id) {
668686
var css = document.createElement("link");
669687
css.setAttribute("rel", "stylesheet");
670688
css.setAttribute("type", "text/css");
671689
css.setAttribute("href", filePath);
690+
if (id) css.setAttribute("id", id);
672691
(document.head || document.documentElement).appendChild(css);
673692
}
674693
function getTrustedPolicy() {
@@ -939,7 +958,7 @@ Call UseGlobal directly, no need to import
939958
let res = await fetchByPassOrigin(url, {
940959
method: "HEAD",
941960
});
942-
if (res.redirected) {
961+
if (res?.redirected) {
943962
console.log("redirected:", url, "->", res.url);
944963
url = res.url;
945964
} else {

0 commit comments

Comments
 (0)