Skip to content

Commit 142b0bd

Browse files
committed
Add content script for tracking
1 parent 5f76886 commit 142b0bd

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/background.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import browser from "webextension-polyfill";
22

3-
console.log("Hello from the background!");
3+
console.log("Start background script");
44

55
browser.runtime.onInstalled.addListener((details) => {
66
console.log("Extension installed:", details);

src/manifest.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"{{firefox}}.manifest_version": 2,
44
"name": "Web Activity Time Tracker",
55
"short_name": "Web Tracker",
6-
"version": "1.7.2",
7-
"description": "Track and limit time your activity in the browser every day.",
6+
"version": "2.0.0",
7+
"description": "Track and limit time your activity in the browser every day. Control how much time you spend on websites.",
88
"options_page": "src/options.html",
99
"icons": {
1010
"16": "src/icons/16x16.png",
@@ -27,6 +27,14 @@
2727
"https://www.netflix.com/*"
2828
],
2929
"offline_enabled": true,
30+
"content_scripts": [
31+
{
32+
"matches": ["*://*/*"],
33+
"js": ["src/tracker.ts"],
34+
"all_frames": true,
35+
"run_at": "document_start"
36+
}
37+
],
3038
"background": {
3139
"{{chrome}}.service_worker": "src/background.ts",
3240
"{{firefox}}.scripts": ["src/background.ts"]

src/tracker.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Browser from 'webextension-polyfill';
2+
import { isValidPage } from './compositions/valid-page';
3+
import { extractHostname } from './compositions/extract-hostname';
4+
5+
export async function initTracker(){
6+
const window = await Browser.windows.getLastFocused({ populate: true });
7+
if (window.focused){
8+
const activeTab = window.tabs?.find(t => t.active === true);
9+
if (isValidPage(activeTab)) {
10+
var activeUrl = extractHostname(activeTab?.url);
11+
var tab = activity.getTab(activeUrl);
12+
if (tab === undefined) {
13+
activity.addTab(activeTab);
14+
}
15+
16+
if (activity.isInBlackList(activeUrl)) {
17+
chrome.action.setBadgeBackgroundColor({ color: '#fdb8b8' })
18+
chrome.action.setBadgeText({
19+
tabId: activeTab.id,
20+
text: 'n/a'
21+
});
22+
} else {
23+
if (tab !== undefined) {
24+
if (currentTab !== tab.url) {
25+
activity.setCurrentActiveTab(tab.url);
26+
}
27+
chrome.idle.queryState(parseInt(setting_interval_inactivity), function(state) {
28+
if (state === 'active') {
29+
mainTRacker(activeUrl, tab, activeTab);
30+
} else checkDOM(state, activeUrl, tab, activeTab);
31+
});
32+
}
33+
}
34+
}
35+
} else activity.closeIntervalForCurrentTab();
36+
}

0 commit comments

Comments
 (0)