Skip to content

Commit 02c4849

Browse files
committed
optimize + tracking
1 parent 97a18ac commit 02c4849

File tree

11 files changed

+60
-46
lines changed

11 files changed

+60
-46
lines changed

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
{
4949
"matches": ["<all_urls>"],
5050
"js": [
51-
"scripts/content-scripts/ufs_global_webpage_context.js",
52-
"scripts/content-scripts/run_scripts.js"
51+
"scripts/content-scripts/ufs_global.js",
52+
"scripts/content-scripts/page_scripts.js"
5353
],
5454
"run_at": "document_start",
5555
"world": "MAIN",

popup/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
runScriptInCurrentTab,
99
sendEventToTab,
1010
toggleActiveScript,
11+
trackingUseScript,
1112
} from "../scripts/helpers/utils.js";
1213
import { checkForUpdate } from "./helpers/checkForUpdate.js";
1314
import { getFlag, t, toggleLang } from "./helpers/lang.js";
@@ -156,7 +157,7 @@ function createScriptButton(script, isFavorite = false) {
156157
checkmark.className = "checkmark tooltip";
157158
checkmark.onclick = async (e) => {
158159
let newValue = await toggleActiveScript(script.id);
159-
newValue && UfsGlobal.Extension.updateScriptClickCount(script.id);
160+
newValue && trackingUseScript(script.id);
160161
newValue ? script.onEnable?.() : script.onDisable?.();
161162
updateButtonChecker(script, buttonContainer, newValue);
162163
};
@@ -320,7 +321,7 @@ async function runScript(script) {
320321
if (willRun) {
321322
try {
322323
recentScriptsSaver.add(script);
323-
UfsGlobal.Extension.updateScriptClickCount(script.id);
324+
trackingUseScript(script.id);
324325
if (isFunction(script.onClickExtension)) await script.onClickExtension();
325326
if (isFunction(script.onClick))
326327
await runScriptInCurrentTab(script.onClick);

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_webpage_context.js").then(() => {
6-
console.log("loaded ufs_global_webpage_context");
7-
UfsGlobal.Extension.updateScriptClickCount("ufs_open_extension_popup");
8-
});

scripts/background-scripts/background-script.js

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
convertBlobToBase64,
44
runScriptInTab,
55
getAllActiveScriptIds,
6+
trackingUseScript,
67
} from "../helpers/utils.js";
78

89
const { ISOLATED, MAIN } = chrome.scripting.ExecutionWorld;
@@ -22,7 +23,7 @@ function runScripts(tabId, event, world) {
2223
runScriptInTab({
2324
tabId: tabId,
2425
func: (scriptIds, event, path) => {
25-
window.ufs_runScritps?.(scriptIds, event, path);
26+
window.runScripts?.(scriptIds, event, path);
2627
},
2728
args: [CACHED.activeScriptIds, event, CACHED.path],
2829
world,
@@ -31,6 +32,7 @@ function runScripts(tabId, event, world) {
3132

3233
const global = {
3334
log: console.log,
35+
trackingUseScript,
3436
async fetch(url, options) {
3537
const res = await fetch(url, options);
3638
let body;
@@ -65,25 +67,6 @@ const global = {
6567
console.log("Response from background script:", data);
6668
return data;
6769
},
68-
async updateScriptClickCount(scriptId) {
69-
console.log("updateScriptClickCount", scriptId);
70-
// return;
71-
try {
72-
let res = await fetch(
73-
"https://useful-script-statistic.glitch.me/count",
74-
// "https://useful-script-statistic.onrender.com/count",
75-
{
76-
method: "POST",
77-
headers: { "Content-Type": "application/json" },
78-
body: JSON.stringify({ script: scriptId }),
79-
}
80-
);
81-
return await res.text();
82-
} catch (e) {
83-
console.log("ERROR update script click count: ", e);
84-
return null;
85-
}
86-
},
8770
};
8871

8972
function main() {
@@ -197,8 +180,9 @@ function main() {
197180
});
198181

199182
chrome.runtime.onInstalled.addListener(function () {
200-
global.updateScriptClickCount("ufs-installed");
201-
console.log("install");
183+
global.trackingUseScript("ufs-installed");
184+
console.log("installed", version);
185+
202186
chrome.contextMenus.create({
203187
title: "Magnify this image",
204188
contexts: ["image"],

scripts/content-scripts/content_script.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import("./ufs_global_webpage_context.js");
1+
import("./ufs_global.js");
22

33
// communication between page-script and content-script
44
function sendToPageScript(event, uuid, data) {
@@ -10,7 +10,7 @@ function sendToPageScript(event, uuid, data) {
1010
);
1111
}
1212

13-
window.ufs_runScritps = runScripts;
13+
window.runScripts = runScripts;
1414
function runScripts(scriptIds, event, path) {
1515
for (let scriptId of scriptIds) {
1616
runScript(scriptId, event);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
runScripts(CACHED.activeScriptIds, detail.event, CACHED.path);
1111
});
1212

13-
window.ufs_runScritps = runScripts;
13+
window.runScripts = runScripts;
1414
function runScripts(scriptIds, event, path) {
1515
CACHED.activeScriptIds = scriptIds;
1616
CACHED.path = path;

scripts/content-scripts/ufs_global_webpage_context.js renamed to scripts/content-scripts/ufs_global.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,21 @@ UfsGlobal.Extension = {
3434
});
3535
},
3636
fetchByPassOrigin(url, options = {}) {
37-
// same origin case
38-
if (location.hostname == new URL(url)?.hostname) {
39-
return fetch(url, options);
40-
}
41-
return UfsGlobal.Extension.runInBackground("fetch", [url, options]);
37+
return new Promise((resolve, reject) => {
38+
let urlObject = new URL(url);
39+
// https://stackoverflow.com/a/9375786/23648002
40+
if (location.hostname == urlObject?.hostname) {
41+
url = urlObject.pathname;
42+
}
43+
fetch(url, options)
44+
.then(resolve)
45+
.catch((e) => {
46+
console.log("NORMAL FETCH FAIL: ", e);
47+
UfsGlobal.Extension.runInBackground("fetch", [url, options])
48+
.then(resolve)
49+
.catch(reject);
50+
});
51+
});
4252
},
4353
getURL(filePath) {
4454
return UfsGlobal.Extension.runInContentScript("chrome.runtime.getURL", [
@@ -50,10 +60,8 @@ UfsGlobal.Extension = {
5060
options,
5161
]);
5262
},
53-
updateScriptClickCount(scriptId) {
54-
return UfsGlobal.Extension.runInBackground("updateScriptClickCount", [
55-
scriptId,
56-
]);
63+
trackingUseScript(scriptId) {
64+
return UfsGlobal.Extension.runInBackground("trackingUseScript", [scriptId]);
5765
},
5866
};
5967
UfsGlobal.DOM = {

scripts/fb_messengerHistory.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1.0">
88
<title>Useful script - Facebook messenger history</title>
99

10-
<script src="./content-scripts/ufs_global_webpage_context.js"></script>
10+
<script src="./content-scripts/ufs_global.js"></script>
1111
<link rel="stylesheet" href="./fb_messengerHistory.css">
1212
</head>
1313

scripts/helpers/utils.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,29 @@
1+
// Utils used by popup and background-script
2+
3+
const { version } = chrome.runtime.getManifest();
4+
5+
export async function trackingUseScript(scriptId) {
6+
console.log("trackingUseScript", scriptId);
7+
try {
8+
let res = await fetch(
9+
"https://useful-script-statistic.glitch.me/count",
10+
// "https://useful-script-statistic.onrender.com/count",
11+
{
12+
method: "POST",
13+
headers: { "Content-Type": "application/json" },
14+
body: JSON.stringify({
15+
script: scriptId,
16+
version: version,
17+
}),
18+
}
19+
);
20+
return await res.text();
21+
} catch (e) {
22+
console.log("ERROR update script click count: ", e);
23+
return null;
24+
}
25+
}
26+
127
export async function sendEventToTab(tabId, data) {
228
console.log("... Sending ", data, " to tab...");
329
const response = await chrome.tabs.sendMessage(tabId, data);

scripts/youtube_localDownloader.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<title>Useful Script - YouTube Local Downloader</title>
88

99
<script src="./libs/file-saver/index.js"></script>
10-
<script src="./content-scripts/ufs_global_webpage_context.js"></script>
10+
<script src="./content-scripts/ufs_global.js"></script>
1111
</head>
1212

1313
<body>

0 commit comments

Comments
 (0)