From 601566778b8388ffaab112604c7a8f4b4b76a370 Mon Sep 17 00:00:00 2001 From: James Anderson Date: Thu, 21 Jan 2016 20:46:14 -0500 Subject: [PATCH] Added option to delay first/every page of site Added option for user to choose whether every page or only first accessed page of site is blocked when using delaying page. --- chrome/content/browser.js | 32 +++++++++++++++++++++--------- chrome/content/core.js | 19 ++++++++++-------- chrome/content/lockdown.js | 5 +++-- chrome/content/options.js | 15 +++++++++++++- chrome/content/options.xul | 30 ++++++++++++++++++++++++++++ chrome/locale/cs-CZ/leechblock.dtd | 1 + chrome/locale/en-US/leechblock.dtd | 1 + chrome/locale/fr-FR/leechblock.dtd | 1 + chrome/locale/pt-BR/leechblock.dtd | 1 + chrome/locale/zh-CN/leechblock.dtd | 1 + chrome/locale/zh-TW/leechblock.dtd | 1 + defaults/preferences/leechblock.js | 5 ++++- 12 files changed, 91 insertions(+), 21 deletions(-) diff --git a/chrome/content/browser.js b/chrome/content/browser.js index cc3fcbc..0442246 100644 --- a/chrome/content/browser.js +++ b/chrome/content/browser.js @@ -165,11 +165,16 @@ LeechBlock.onPageLoad = function (event) { return; } - // Clear preference for allowed host (unless this page is on allowed host) - if (/^http|file|about/.test(parsedURL.protocol) - && (win.frameElement == null) - && parsedURL.host != LeechBlock.getUniCharPref("ah")) { - LeechBlock.clearUserPref("ah"); + // Clear preference for allowed origin/page if this is different origin/page + let ao = LeechBlock.getUniCharPref("ao"); + let ap = LeechBlock.getUniCharPref("ap"); + if (/^http|file|about/.test(parsedURL.protocol) && (win.frameElement == null)) { + if (parsedURL.origin != ao) { + LeechBlock.clearUserPref("ao"); + } + if (parsedURL.page != ap) { + LeechBlock.clearUserPref("ap"); + } } // Hide extension in about:addons (if option selected) @@ -233,6 +238,7 @@ LeechBlock.onPageLoad = function (event) { let countdown = { win: win, blockedURL: blockedURL, + blockedSet: blockedSet, delaySecs: delaySecs }; doc.leechblockCountdownInterval = setInterval( @@ -288,8 +294,10 @@ LeechBlock.checkWindow = function (parsedURL, win, isRepeat) { return; } - // Quick exit for allowed host - if (parsedURL.host == LeechBlock.getUniCharPref("ah")) { + // Quick exit for allowed origin/page + let ao = LeechBlock.getUniCharPref("ao"); + let ap = LeechBlock.getUniCharPref("ap"); + if (parsedURL.origin == ao || parsedURL.page == ap) { return; } @@ -785,8 +793,14 @@ LeechBlock.onCountdownTimer = function (countdown) { // Get parsed URL for blocked page let parsedURL = LeechBlock.getParsedURL(countdown.blockedURL); - // Set preference for allowed host - LeechBlock.setUniCharPref("ah", parsedURL.host); + // Set preference for allowed origin/page + if (LeechBlock.getBitPref("delayFirst", countdown.blockedSet)) { + LeechBlock.setUniCharPref("ao", parsedURL.origin); + LeechBlock.clearUserPref("ap"); + } else { + LeechBlock.setUniCharPref("ap", parsedURL.page); + LeechBlock.clearUserPref("ao"); + } // Continue to blocked page win.location = countdown.blockedURL; diff --git a/chrome/content/core.js b/chrome/content/core.js index 3696d89..9827780 100644 --- a/chrome/content/core.js +++ b/chrome/content/core.js @@ -146,21 +146,23 @@ LeechBlock.updatePreferences = function () { // Returns parsed URL (page address, arguments, and hash) // LeechBlock.getParsedURL = function (url) { - const PARSE_URL = /^((\w+):\/*(\w+(?::\w+)?@)?([\w-\.]+)(?::(\d+))?([^\?#]*))(\?[^#]*)?(#.*)?$/; + const PARSE_URL = /^(((\w+):\/*(\w+(?::\w+)?@)?([\w-\.]+)(?::(\d*))?)([^\?#]*))(\?[^#]*)?(#.*)?$/; let results = PARSE_URL.exec(url); if (results != null) { let page = results[1]; - let protocol = results[2]; - let userinfo = results[3]; - let host = results[4]; - let port = results[5]; - let path = results[6]; - let query = results[7]; - let fragment = results[8]; + let origin = results[2]; + let protocol = results[3]; + let userinfo = results[4]; + let host = results[5]; + let port = results[6]; + let path = results[7]; + let query = results[8]; + let fragment = results[9]; return { pageNoArgs: page, page: (query == null) ? page : (page + query), + origin: origin, protocol: protocol, host: host, path: path, @@ -172,6 +174,7 @@ LeechBlock.getParsedURL = function (url) { return { pageNoArgs: null, page: null, + origin: null, protocol: null, host: null, path: null, diff --git a/chrome/content/lockdown.js b/chrome/content/lockdown.js index 4b39a6e..a6e740c 100644 --- a/chrome/content/lockdown.js +++ b/chrome/content/lockdown.js @@ -79,8 +79,9 @@ LeechBlock.lockdownOK = function () { LeechBlock.setCharPref("timedata" + set, timedata.join(",")); } - // Clear preference for allowed host - LeechBlock.clearUserPref("ah"); + // Clear preference for allowed origin/page + LeechBlock.clearUserPref("ao"); + LeechBlock.clearUserPref("ap"); return true; } diff --git a/chrome/content/options.js b/chrome/content/options.js index 7de343f..eb1f642 100644 --- a/chrome/content/options.js +++ b/chrome/content/options.js @@ -134,6 +134,7 @@ LeechBlock.optionsInit = function () { let prevAddons = LeechBlock.getBitPref("prevAddons", set); let prevConfig = LeechBlock.getBitPref("prevConfig", set); let countFocus = LeechBlock.getBitPref("countFocus", set); + let delayFirst = LeechBlock.getBitPref("delayFirst", set); let delaySecs = LeechBlock.getCharPref("delaySecs" + set); let times = LeechBlock.getCharPref("times" + set); let limitMins = LeechBlock.getCharPref("limitMins" + set); @@ -151,6 +152,7 @@ LeechBlock.optionsInit = function () { document.getElementById("lb-prev-addons" + set).checked = prevAddons; document.getElementById("lb-prev-config" + set).checked = prevConfig; document.getElementById("lb-count-focus" + set).checked = countFocus; + document.getElementById("lb-delay-first" + set).checked = delayFirst; document.getElementById("lb-delay-secs" + set).value = delaySecs; document.getElementById("lb-times" + set).value = times; document.getElementById("lb-limit-mins" + set).value = limitMins; @@ -256,6 +258,7 @@ LeechBlock.optionsOK = function () { let prevAddons = document.getElementById("lb-prev-addons" + set).checked; let prevConfig = document.getElementById("lb-prev-config" + set).checked; let countFocus = document.getElementById("lb-count-focus" + set).checked; + let delayFirst = document.getElementById("lb-delay-first" + set).checked; let delaySecs = document.getElementById("lb-delay-secs" + set).value; let times = document.getElementById("lb-times" + set).value; let limitMins = document.getElementById("lb-limit-mins" + set).value; @@ -289,6 +292,7 @@ LeechBlock.optionsOK = function () { LeechBlock.setBitPref("prevAddons", set, prevAddons); LeechBlock.setBitPref("prevConfig", set, prevConfig); LeechBlock.setBitPref("countFocus", set, countFocus); + LeechBlock.setBitPref("delayFirst", set, delayFirst); LeechBlock.setCharPref("delaySecs" + set, delaySecs); LeechBlock.setUniCharPref("blockRE" + set, regexps.block); LeechBlock.setUniCharPref("allowRE" + set, regexps.allow); @@ -341,7 +345,7 @@ LeechBlock.optionsCancel = function () { LeechBlock.disableSetOptions = function (set) { let items = [ "sites", "sites-URL", - "active-block", "count-focus", "delay-secs", + "active-block", "count-focus", "delay-first", "delay-secs", "times", "all-day", "limit-mins", "limit-period", "mode", "day0", "day1", "day2", "day3", "day4", "day5", "day6", "every-day", @@ -479,6 +483,7 @@ LeechBlock.exportOptions = function () { let prevAddons = document.getElementById("lb-prev-addons" + set).checked; let prevConfig = document.getElementById("lb-prev-config" + set).checked; let countFocus = document.getElementById("lb-count-focus" + set).checked; + let delayFirst = document.getElementById("lb-delay-first" + set).checked; let delaySecs = document.getElementById("lb-delay-secs" + set).value; let times = document.getElementById("lb-times" + set).value; let limitMins = document.getElementById("lb-limit-mins" + set).value; @@ -499,6 +504,7 @@ LeechBlock.exportOptions = function () { text += "prevAddons" + set + "=" + prevAddons+ "\n"; text += "prevConfig" + set + "=" + prevConfig + "\n"; text += "countFocus" + set + "=" + countFocus + "\n"; + text += "delayFirst" + set + "=" + delayFirst + "\n"; text += "delaySecs" + set + "=" + delaySecs + "\n"; text += "times" + set + "=" + times + "\n"; text += "limitMins" + set + "=" + limitMins + "\n"; @@ -591,6 +597,7 @@ LeechBlock.importOptions = function () { let prevAddons = opts["prevAddons" + set]; let prevConfig = opts["prevConfig" + set]; let countFocus = opts["countFocus" + set]; + let delayFirst = opts["delayFirst" + set]; let delaySecs = opts["delaySecs" + set]; let times = opts["times" + set]; let limitMins = opts["limitMins" + set] @@ -649,6 +656,12 @@ LeechBlock.importOptions = function () { element.checked = isTrue(countFocus); } } + if (delayFirst != undefined) { + let element = document.getElementById("lb-delay-first" + set); + if (!element.disabled) { + element.checked = isTrue(delayFirst); + } + } if (delaySecs != undefined) { let element = document.getElementById("lb-delay-secs" + set); if (!element.disabled) { diff --git a/chrome/content/options.xul b/chrome/content/options.xul index fafa992..2dc2968 100644 --- a/chrome/content/options.xul +++ b/chrome/content/options.xul @@ -258,6 +258,11 @@ label="&options.countFocusLabel;"/> + + + + &options.delaySecsDesc1; @@ -475,6 +480,11 @@ label="&options.countFocusLabel;"/> + + + + &options.delaySecsDesc1; @@ -692,6 +702,11 @@ label="&options.countFocusLabel;"/> + + + + &options.delaySecsDesc1; @@ -909,6 +924,11 @@ label="&options.countFocusLabel;"/> + + + + &options.delaySecsDesc1; @@ -1126,6 +1146,11 @@ label="&options.countFocusLabel;"/> + + + + &options.delaySecsDesc1; @@ -1343,6 +1368,11 @@ label="&options.countFocusLabel;"/> + + + + &options.delaySecsDesc1; diff --git a/chrome/locale/cs-CZ/leechblock.dtd b/chrome/locale/cs-CZ/leechblock.dtd index 58bb639..354a16a 100644 --- a/chrome/locale/cs-CZ/leechblock.dtd +++ b/chrome/locale/cs-CZ/leechblock.dtd @@ -77,6 +77,7 @@ + diff --git a/chrome/locale/en-US/leechblock.dtd b/chrome/locale/en-US/leechblock.dtd index 75e987d..9583381 100644 --- a/chrome/locale/en-US/leechblock.dtd +++ b/chrome/locale/en-US/leechblock.dtd @@ -77,6 +77,7 @@ + diff --git a/chrome/locale/fr-FR/leechblock.dtd b/chrome/locale/fr-FR/leechblock.dtd index fb4c968..19848db 100644 --- a/chrome/locale/fr-FR/leechblock.dtd +++ b/chrome/locale/fr-FR/leechblock.dtd @@ -77,6 +77,7 @@ + diff --git a/chrome/locale/pt-BR/leechblock.dtd b/chrome/locale/pt-BR/leechblock.dtd index 4c9ef6f..f74f0a0 100644 --- a/chrome/locale/pt-BR/leechblock.dtd +++ b/chrome/locale/pt-BR/leechblock.dtd @@ -77,6 +77,7 @@ + diff --git a/chrome/locale/zh-CN/leechblock.dtd b/chrome/locale/zh-CN/leechblock.dtd index 99db18f..9253dd6 100644 --- a/chrome/locale/zh-CN/leechblock.dtd +++ b/chrome/locale/zh-CN/leechblock.dtd @@ -77,6 +77,7 @@ + diff --git a/chrome/locale/zh-TW/leechblock.dtd b/chrome/locale/zh-TW/leechblock.dtd index c2125ef..4ccdd2f 100644 --- a/chrome/locale/zh-TW/leechblock.dtd +++ b/chrome/locale/zh-TW/leechblock.dtd @@ -77,6 +77,7 @@ + diff --git a/defaults/preferences/leechblock.js b/defaults/preferences/leechblock.js index aeb7d1f..f41d8c8 100644 --- a/defaults/preferences/leechblock.js +++ b/defaults/preferences/leechblock.js @@ -68,6 +68,7 @@ pref("extensions.leechblock.blockURL5", "chrome://leechblock/content/blocked.xht pref("extensions.leechblock.blockURL6", "chrome://leechblock/content/blocked.xhtml?$S&$U"); pref("extensions.leechblock.activeBlock", 0); pref("extensions.leechblock.countFocus", 63); +pref("extensions.leechblock.delayFirst", 63); pref("extensions.leechblock.delaySecs1", "60"); pref("extensions.leechblock.delaySecs2", "60"); pref("extensions.leechblock.delaySecs3", "60"); @@ -95,7 +96,8 @@ pref("extensions.leechblock.timedata6", ""); pref("extensions.leechblock.repeatCheckPeriod", 5000); pref("extensions.leechblock.lockdownDuration", 0); pref("extensions.leechblock.lockdownSets", 0); -pref("extensions.leechblock.ah", ""); +pref("extensions.leechblock.ao", ""); +pref("extensions.leechblock.ap", ""); pref("services.sync.prefs.sync.extensions.leechblock.setName1", true); pref("services.sync.prefs.sync.extensions.leechblock.setName2", true); pref("services.sync.prefs.sync.extensions.leechblock.setName3", true); @@ -165,6 +167,7 @@ pref("services.sync.prefs.sync.extensions.leechblock.blockURL5", true); pref("services.sync.prefs.sync.extensions.leechblock.blockURL6", true); pref("services.sync.prefs.sync.extensions.leechblock.activeBlock", true); pref("services.sync.prefs.sync.extensions.leechblock.countFocus", true); +pref("services.sync.prefs.sync.extensions.leechblock.delayFirst", true); pref("services.sync.prefs.sync.extensions.leechblock.delaySecs1", true); pref("services.sync.prefs.sync.extensions.leechblock.delaySecs2", true); pref("services.sync.prefs.sync.extensions.leechblock.delaySecs3", true);