-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f9aa12e
commit d1adad3
Showing
5 changed files
with
77 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}"e=${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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.