Skip to content

Commit 4b960fe

Browse files
committed
Refactored extension to block based on tabs so service workers (like used on Twitter) still get blocked
1 parent 9d9fa71 commit 4b960fe

File tree

2 files changed

+46
-20
lines changed

2 files changed

+46
-20
lines changed

extension/focus.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,14 @@ var redirectURL;
1111
var regexSites = [];
1212
var compiledRegexSites = [];
1313

14-
function onBeforeRequestHandler(info) {
15-
if (info.url.indexOf("?focus_url=") != -1) {
16-
return {};
17-
}
18-
19-
if (urlIsBlocked(info.url, compiledRegexSites, isWhitelist)) {
20-
if (enableCloseBrowserTabs) {
21-
chrome.tabs.remove(info.tabId);
22-
} else {
23-
var url = redirectURL + "?focus_url=" + encodeURIComponent(info.url);
24-
return {redirectUrl: url};
25-
}
26-
}
27-
}
28-
2914
function reset() {
3015
isWhitelist = false;
3116
isFocusing = false;
3217
regexSites = [];
3318
compiledRegexSites = [];
34-
vendor.webRequest.onBeforeRequest.removeListener(onBeforeRequestHandler);
3519
}
3620

3721
conn.focus = function(data) {
38-
vendor.webRequest.onBeforeRequest.removeListener(onBeforeRequestHandler);
3922

4023
regexSites = [];
4124
compiledRegexSites = [];
@@ -55,9 +38,10 @@ conn.focus = function(data) {
5538

5639
var filters = {urls: ["<all_urls>"], types: ["main_frame", "sub_frame"]};
5740
var extraInfoSpec = ["blocking"];
58-
vendor.webRequest.onBeforeRequest.addListener(onBeforeRequestHandler, filters, extraInfoSpec);
5941

6042
reloadShouldBeBlockedPages(compiledRegexSites);
43+
44+
processTabs();
6145
};
6246

6347
conn.unfocus = function() {
@@ -73,3 +57,45 @@ conn.cleanup = function() {
7357

7458
conn.connect();
7559

60+
function handleBeforeNavigate(navDetails) {
61+
if (!isFocusing) { return }
62+
//console.log("handleBeforeNavigate");
63+
64+
if (navDetails.frameId == 0) {
65+
checkTabURL(navDetails.tabId, navDetails.url);
66+
}
67+
}
68+
69+
function processTabs() {
70+
//console.log("processTabs");
71+
72+
browser.tabs.query({}, function(tabs) {
73+
if (browser.runtime.lastError) {
74+
console.log("error fetching tabs", error);
75+
return;
76+
}
77+
78+
for (let tab of tabs) {
79+
checkTabURL(tab.id, tab.url);
80+
}
81+
});
82+
}
83+
84+
function checkTabURL(tabId, url) {
85+
if (url.indexOf("about:") == 0) {
86+
return false;
87+
}
88+
89+
if (urlIsBlocked(url, compiledRegexSites, isWhitelist)) {
90+
if (enableCloseBrowserTabs) {
91+
chrome.tabs.remove(tabId);
92+
} else {
93+
var newURL = redirectURL + "?focus_url=" + encodeURIComponent(url);
94+
chrome.tabs.update(tabId, { url: newURL });
95+
}
96+
return true;
97+
}
98+
}
99+
100+
browser.webNavigation.onBeforeNavigate.addListener(handleBeforeNavigate);
101+

extension/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"name": "Focus",
33
"author": "Brad Jasper",
4-
"version": "2.4",
4+
"version": "2.5",
55
"manifest_version": 2,
66
"description": "Official browser extension for Focus, a macOS app that blocks distracting websites and applications so you can be more productive.",
77
"icons": {
88
"16": "icon_16.png",
99
"48": "icon_48.png",
1010
"128": "icon_128.png"
1111
},
12-
"permissions": ["webRequest", "webRequestBlocking", "tabs", "<all_urls>"],
12+
"permissions": ["webNavigation", "tabs"],
1313
"background": {
1414
"scripts": ["browserdetect.js", "config.js", "utils.js", "reconnecting-websocket.js", "focus-connection.js", "focus.js"]
1515
}

0 commit comments

Comments
 (0)