Skip to content

Commit

Permalink
converted processing to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjasper committed Sep 8, 2023
1 parent f9aa12e commit d1adad3
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 231 deletions.
6 changes: 5 additions & 1 deletion extension/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const vendor = (BrowserDetect.browser == "Firefox" ? browser : chrome);
const manifestData = vendor.runtime.getManifest();
const version = manifestData.version;

var config = {};
config.version = "2.8.0";
config.version = version;
config.min_port = 8913;
config.max_port = 8918;
config.host_local = "localhost";
Expand Down
45 changes: 33 additions & 12 deletions extension/focus-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//
function FocusConnection() {

this.isFocusing = false;
this.version = "0.0";
this.platform = "unknown";
this.min_port = config.min_port;
Expand All @@ -21,16 +22,36 @@ function FocusConnection() {

console.log("Creating new Focus connection to " + endpoint);

var allowedMsgs = ["focus", "unfocus"];

// Overridden by browser extensions
this.focus = function() {};
this.unfocus = function() {};
this.cleanup = function() {};
var allowedMsgs = ["focus", "unfocus", "block"];

var ws = new ReconnectingWebSocket(endpoint);

ws.ping = function() {
this.focus = () => {
console.log("Focus");
this.isFocusing = true;
this.onfocus();
};

this.unfocus = function () {
console.log("Unfocus");
this.isFocusing = false;
};

this.cleanup = function () {
console.log("Cleanup");
this.isFocusing = false;
};

this.check = function (tabId, url) {
if (!this.isFocusing) return false;
ws.send(JSON.stringify({
"msg": "check",
"tabId": tabId,
"url": url
}));
};

ws.ping = function () {
console.log("Sending ping to Focus");
ws.send(JSON.stringify({
"msg": "ping",
Expand All @@ -39,12 +60,12 @@ function FocusConnection() {
}));
};

ws.onopen = function() {
ws.onopen = function () {
console.log("Websocket is open");
ws.ping();
};
}

ws.onerror = function(err) {
ws.onerror = function (err) {
console.log("Websocket error: " + err);
self.port = self.port - 1;
if (self.port < self.min_port) {
Expand All @@ -55,12 +76,12 @@ function FocusConnection() {
self.cleanup();
};

ws.onclose = function() {
ws.onclose = function () {
console.log("Websocket is closed");
self.cleanup();
};

ws.onmessage = function(evt) {
ws.onmessage = function (evt) {
console.log("Received message from server");

try {
Expand Down
121 changes: 39 additions & 82 deletions extension/focus.js
Original file line number Diff line number Diff line change
@@ -1,103 +1,60 @@
var vendor = getBrowserType();
const blockURL = vendor.extension.getURL("/block.html");

var conn = new FocusConnection();
const conn = new FocusConnection();
conn.version = config.version;
conn.platform = BrowserDetect.browser;

var isWhitelist = false;
var isFocusing = false;
var enableCloseBrowserTabs = false;
var redirectURL;
var regexSites = [];
var compiledRegexSites = [];

function reset() {
isWhitelist = false;
isFocusing = false;
regexSites = [];
compiledRegexSites = [];
}

conn.focus = function (data) {

regexSites = [];
compiledRegexSites = [];

if (!data.regexSites || data.regexSites.length == 0) {
return;
}

console.log("Focusing");

isWhitelist = data.whitelist;
regexSites = data.regexSites;
compiledRegexSites = compileRegexSites(data.regexSites);
isFocusing = true;
redirectURL = data.redirectURL;

var filters = { urls: ["<all_urls>"], types: ["main_frame", "sub_frame"] };
var extraInfoSpec = ["blocking"];

reloadShouldBeBlockedPages(compiledRegexSites);

processTabs();
};

conn.unfocus = function () {
console.log("Unfocusing");
reset();
reloadBlockedPages();
};

conn.cleanup = function () {
console.log("Cleaning up request handler");
reset();
};

conn.connect();

conn.focus({ regexSites: [{ regexUrlStr: ".*reddit.com.*" }] });
function processFrontmostTab() {
console.log("Processing front-most tab");
vendor.windows.getCurrent({ populate: true }, function (currentWindow) {
for (var i = 0; i < currentWindow.tabs.length; i++) {
const tab = currentWindow.tabs[i];
if (tab.active) {
processTab(tab.id, tab.url);
break;
}
}
});
}

function handleBeforeNavigate(navDetails) {
if (!isFocusing) { return }
//console.log("handleBeforeNavigate");
if (!conn.isFocusing) { return }

if (navDetails.frameId == 0) {
checkTabURL(navDetails.tabId, navDetails.url);
processTab(navDetails.tabId, navDetails.url);
}
}

function processTabs() {
//console.log("processTabs");
function convertRedirectURLToLocalTemplate(redirectURL) {
const url = new URL(redirectURL);
const templateURL = new URL(blockURL);
templateURL.search = url.search;
return templateURL.toString();
}

vendor.tabs.query({}, function (tabs) {
if (vendor.runtime.lastError) {
console.log("error fetching tabs", error);
return;
}

for (let tab of tabs) {
checkTabURL(tab.id, tab.url);
}
});
function processTab(tabId, url) {
if (url.indexOf(blockURL) == 0) return;
conn.check(tabId, url);
}

function checkTabURL(tabId, url) {
if (url.indexOf("about:") == 0) {
return false;
}
vendor.webNavigation.onBeforeNavigate.addListener(handleBeforeNavigate);

if (urlIsBlocked(url, compiledRegexSites, isWhitelist)) {
const quote = "Hello";
const author = "World";
var blockURL = chrome.extension.getURL('/block.html');
var newURL = `${blockURL}?url=${encodeURIComponent(url)}&quote=${encodeURIComponent(quote)}&author=${encodeURIComponent(author)}`;
chrome.tabs.update(tabId, { url: newURL });
return true;
}
conn.block = function (data) {
if (!data.url) return;
if (!data.redirectURL) return;
if (!data.tabId) return;
console.log(`blocking ${data.url}`);

return false;
}
const redirectURL = convertRedirectURLToLocalTemplate(data.redirectURL);

vendor.webNavigation.onBeforeNavigate.addListener(handleBeforeNavigate);
vendor.tabs.update(data.tabId, { url: redirectURL });
};

conn.onfocus = function () {
processFrontmostTab();
}

conn.connect();
1 change: 0 additions & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"scripts": [
"browserdetect.js",
"config.js",
"utils.js",
"reconnecting-websocket.js",
"focus-connection.js",
"focus.js"
Expand Down
135 changes: 0 additions & 135 deletions extension/utils.js

This file was deleted.

0 comments on commit d1adad3

Please sign in to comment.