Skip to content

Commit 231ed81

Browse files
committed
Add content script for check to playing video
1 parent 479a203 commit 231ed81

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

manifest.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
"name": "Web Activity Time Tracker",
55
"short_name": "Web Activity",
6-
"version": "0.8.0",
6+
"version": "0.8.1",
77
"minimum_chrome_version": "26",
88

99
"description": "Track your activity in the browser.",
@@ -23,10 +23,16 @@
2323
"idle",
2424
"chrome://favicon/*",
2525
"webNavigation",
26-
"unlimitedStorage",
27-
"<all_urls>"
26+
"unlimitedStorage"
2827
],
2928
"offline_enabled": true,
29+
"content_scripts": [
30+
{
31+
"matches": ["http://*/*", "https://*/*"],
32+
"js": ["scripts/contentScript.js"],
33+
"run_at": "document_end"
34+
}
35+
],
3036
"background": {
3137
"scripts": ["scripts/common.js",
3238
"scripts/storage.js",

scripts/background.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function backgroundCheck() {
3636

3737
if (tab !== undefined) {
3838
activity.setCurrentActiveTab(tab.url);
39+
3940
chrome.idle.queryState(parseInt(setting_interval_inactivity), function (state) {
4041
if (state === 'active') {
4142
mainTRacker(activeUrl, tab, activeTab);
@@ -104,9 +105,12 @@ function isVideoPlayedOnPage() {
104105

105106
function checkDOM(state, activeUrl, tab, activeTab) {
106107
if (state === 'idle') {
107-
chrome.tabs.executeScript({ code: "var videoElement = document.getElementsByTagName('video')[0]; (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2);" }, (results) => {
108-
if (results !== undefined && results[0] !== undefined && results[0] === true)
109-
mainTRacker(activeUrl, tab, activeTab);
108+
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
109+
chrome.tabs.sendMessage(tabs[0].id, { action: 'CHECK_VIDEO' }, function (response) {
110+
if (response !== undefined && response.value !== undefined && response.value === true) {
111+
mainTRacker(activeUrl, tab, activeTab);
112+
}
113+
});
110114
});
111115
}
112116
}

scripts/contentScript.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//start connection in content script
2+
let contentPort = chrome.runtime.connect({
3+
name: 'background-content'
4+
});
5+
6+
//Listen for runtime message
7+
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
8+
if (message.action === 'CHECK_VIDEO') {
9+
var value = isVideoPlayedOnPage();
10+
sendResponse({ value: value });
11+
}
12+
});
13+
14+
function isVideoPlayedOnPage() {
15+
var videoElement = document.getElementsByTagName('video')[0];
16+
if (videoElement !== undefined && videoElement.currentTime > 0 && !videoElement.paused && !videoElement.ended && videoElement.readyState > 2) {
17+
return true;
18+
}
19+
else return false;
20+
}

0 commit comments

Comments
 (0)