Add site blocked alert page

Inform the user about the full url blocking
This commit is contained in:
Kevin Röbert 2017-09-13 23:35:40 +02:00
parent b23089ac9a
commit e528ade2f0
4 changed files with 109 additions and 45 deletions

View File

@ -12,6 +12,7 @@ var badgedStatus;
var tabid = 0; var tabid = 0;
var globalCounter; var globalCounter;
var globalURLCounter; var globalURLCounter;
var siteBlockedAlert = browser.extension.getURL ('./siteBlockedAlert.html');
/** /**
* Initialize the JSON provider object keys. * Initialize the JSON provider object keys.
@ -26,7 +27,7 @@ function getKeys(obj){
/** /**
* Initialize the providers form the JSON object. * Initialize the providers form the JSON object.
* *
*/ */
function createProviders() function createProviders()
{ {
@ -53,12 +54,12 @@ function createProviders()
}; };
/** /**
* Convert the external data to JSON Objects and * Convert the external data to JSON Objects and
* call the create provider function. * call the create provider function.
* *
* @param {String} retrievedText - pure data form github * @param {String} retrievedText - pure data form github
*/ */
function toJSON(retrievedText) { function toJSON(retrievedText) {
data = JSON.parse(retrievedText); data = JSON.parse(retrievedText);
getKeys(data.providers); getKeys(data.providers);
createProviders(); createProviders();
@ -67,11 +68,11 @@ function toJSON(retrievedText) {
/** /**
* Load local saved data, if the browser is offline or * Load local saved data, if the browser is offline or
* some other network trouble. * some other network trouble.
* *
*/ */
function loadOldDataFromStore() function loadOldDataFromStore()
{ {
browser.storage.local.get('ClearURLsData', function(data){ browser.storage.local.get('ClearURLsData', function(data){
if(data.ClearURLsData){ if(data.ClearURLsData){
data = data.ClearURLsData; data = data.ClearURLsData;
} }
@ -85,7 +86,7 @@ function loadOldDataFromStore()
/** /**
* Fetch the Rules & Exception github. * Fetch the Rules & Exception github.
* *
*/ */
function fetchFromURL() function fetchFromURL()
{ {
@ -99,12 +100,12 @@ function fetchFromURL()
{ {
var responseText = response.clone().text().then(function(responseText){ var responseText = response.clone().text().then(function(responseText){
if(response.ok) if(response.ok)
{ {
browser.storage.local.set({"ClearURLsData": responseText}); browser.storage.local.set({"ClearURLsData": responseText});
toJSON(responseText); toJSON(responseText);
} }
else { else {
loadOldDataFromStore(); loadOldDataFromStore();
} }
}); });
}; };
@ -136,7 +137,7 @@ function Provider(_name,_completeProvider = false){
/** /**
* Add URL pattern. * Add URL pattern.
* *
* @require urlPatterns as RegExp * @require urlPatterns as RegExp
*/ */
this.setURLPattern = function(urlPatterns) { this.setURLPattern = function(urlPatterns) {
@ -153,16 +154,16 @@ function Provider(_name,_completeProvider = false){
/** /**
* Check the url is matching the ProviderURL. * Check the url is matching the ProviderURL.
* *
* @return {String} ProviderURL as RegExp * @return {String} ProviderURL as RegExp
*/ */
this.matchURL = function(url) { this.matchURL = function(url) {
return !(this.matchException(url)) && (url.match(urlPattern) != null) && (url.match(urlPattern).length > 0); return !(this.matchException(url)) && (url.match(urlPattern) != null) && (url.match(urlPattern).length > 0);
}; };
/** /**
* Add a rule to the rule array. * Add a rule to the rule array.
* *
* @param String rule RegExp as string * @param String rule RegExp as string
*/ */
this.addRule = function(rule) { this.addRule = function(rule) {
@ -179,7 +180,7 @@ function Provider(_name,_completeProvider = false){
/** /**
* Return all rules as an array. * Return all rules as an array.
* *
* @return Array RegExp strings * @return Array RegExp strings
*/ */
this.getRules = function() { this.getRules = function() {
@ -188,7 +189,7 @@ function Provider(_name,_completeProvider = false){
/** /**
* Add a exception to the exceptions array. * Add a exception to the exceptions array.
* *
* @param String exception RegExp as string * @param String exception RegExp as string
*/ */
this.addException = function(exception) { this.addException = function(exception) {
@ -198,19 +199,22 @@ function Provider(_name,_completeProvider = false){
/** /**
* Private helper method to check if the url * Private helper method to check if the url
* an exception. * an exception.
* *
* @param {String} url RegExp as string * @param {String} url RegExp as string
* @return {boolean} if matching? true: false * @return {boolean} if matching? true: false
*/ */
this.matchException = function(url) { this.matchException = function(url) {
var result = false; var result = false;
//Add the site blocked alert to every exception
if(url == siteBlockedAlert) return true;
for (var i = 0; i < exceptions.length; i++) { for (var i = 0; i < exceptions.length; i++) {
if(result) { break; } if(result) { break; }
result = (url.match(new RegExp(exceptions[i], "gi"))) && (url.match(new RegExp(exceptions[i], "gi")).length > 0); result = (url.match(new RegExp(exceptions[i], "gi"))) && (url.match(new RegExp(exceptions[i], "gi")).length > 0);
} }
return result; return result;
}; };
} }
@ -219,7 +223,7 @@ function Provider(_name,_completeProvider = false){
/** /**
* Helper function which remove the tracking fields * Helper function which remove the tracking fields
* for each provider given as parameter. * for each provider given as parameter.
* *
* @param {Provider} provider Provider-Object * @param {Provider} provider Provider-Object
* @param {webRequest} request webRequest-Object * @param {webRequest} request webRequest-Object
* @return {Array} Array with changes and url fields * @return {Array} Array with changes and url fields
@ -235,7 +239,7 @@ function removeFieldsFormURL(provider, request)
{ {
for (var i = 0; i < rules.length; i++) { for (var i = 0; i < rules.length; i++) {
var bevorReplace = url; var bevorReplace = url;
url = url.replace(new RegExp(rules[i], "gi"), ""); url = url.replace(new RegExp(rules[i], "gi"), "");
if(bevorReplace != url) if(bevorReplace != url)
@ -249,7 +253,7 @@ function removeFieldsFormURL(provider, request)
if(badgedStatus) { if(badgedStatus) {
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid}); browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
} }
else else
{ {
browser.browserAction.setBadgeText({text: "", tabId: tabid}); browser.browserAction.setBadgeText({text: "", tabId: tabid});
} }
@ -268,10 +272,10 @@ function removeFieldsFormURL(provider, request)
if(badgedStatus) { if(badgedStatus) {
browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid}); browser.browserAction.setBadgeText({text: (++badges[tabid]).toString(), tabId: tabid});
} }
else else
{ {
browser.browserAction.setBadgeText({text: "", tabId: tabid}); browser.browserAction.setBadgeText({text: "", tabId: tabid});
} }
cancel = true; cancel = true;
} }
@ -300,7 +304,7 @@ function countFields(url)
/** /**
* Function which called from the webRequest to * Function which called from the webRequest to
* remove the tracking fields from the url. * remove the tracking fields from the url.
* *
* @param {webRequest} request webRequest-Object * @param {webRequest} request webRequest-Object
* @return {Array} redirectUrl or none * @return {Array} redirectUrl or none
*/ */
@ -327,7 +331,7 @@ function clearUrl(request)
"changes": false, "changes": false,
"url": "" "url": ""
}; };
/* /*
* Call for every provider the removeFieldsFormURL method. * Call for every provider the removeFieldsFormURL method.
*/ */
@ -335,12 +339,13 @@ function clearUrl(request)
result = removeFieldsFormURL(providers[i], request); result = removeFieldsFormURL(providers[i], request);
/* /*
* Cancel the Request * Cancel the Request and redirect to the site blocked alert page,
* to inform the user about the full url blocking.
*/ */
if(result["cancel"]){ if(result["cancel"]){
return { return {
cancel: true redirectUrl: siteBlockedAlert
} };
} }
/* /*
@ -351,9 +356,9 @@ function clearUrl(request)
return { return {
redirectUrl: result["url"] redirectUrl: result["url"]
}; };
} }
} }
} }
}; };
/** /**
@ -383,7 +388,7 @@ function setGlobalCounter() {
else { else {
globalURLCounter = 0; globalURLCounter = 0;
} }
}); });
} }
/** /**
@ -440,11 +445,11 @@ browser.tabs.onActivated.addListener(handleActivated);
/** /**
* Call by each Request and checking the url. * Call by each Request and checking the url.
* *
* @type {Array} * @type {Array}
*/ */
browser.webRequest.onBeforeRequest.addListener( browser.webRequest.onBeforeRequest.addListener(
clearUrl, clearUrl,
{urls: ["<all_urls>"]}, {urls: ["<all_urls>"]},
["blocking"] ["blocking"]
); );

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "ClearURLs", "name": "ClearURLs",
"version": "1.1.2.1", "version": "1.1.2.2",
"author": "Kevin R.", "author": "Kevin R.",
"description": "Remove tracking elements form URLs. ", "description": "Remove tracking elements form URLs. ",
"homepage_url": "https://github.com/KevinRoebert/ClearUrls", "homepage_url": "https://github.com/KevinRoebert/ClearUrls",
@ -36,5 +36,8 @@
{ {
"matches": ["<all_urls>"] "matches": ["<all_urls>"]
} }
],
"web_accessible_resources": [
"siteBlockedAlert.html"
] ]
} }

View File

@ -37,14 +37,14 @@
<input type="checkbox" id="tabcounter"> <input type="checkbox" id="tabcounter">
<span class="slider round"></span> <span class="slider round"></span>
<label>Toolbar counter</label> <label>Toolbar counter</label>
</label> </label>
</div> </div>
</div> </div>
<br> <br>
<div class="row"> <div class="row">
<div class="col-sm-1"> <div class="col-sm-1">
<h5>Statistics</h5> <h5>Statistics</h5>
<div class="progress"> <div class="progress">
<div class="progress-bar progress-bar-danger progress-bar-striped" role="progressbar" style="width: 0%" id="progress_blocked"></div> <div class="progress-bar progress-bar-danger progress-bar-striped" role="progressbar" style="width: 0%" id="progress_blocked"></div>
<div class="progress-bar progress-bar-info progress-bar-striped" role="progressbar" style="width: 100%" id="progress_non_blocked"></div> <div class="progress-bar progress-bar-info progress-bar-striped" role="progressbar" style="width: 100%" id="progress_non_blocked"></div>
@ -55,24 +55,24 @@
<h6>Total blocked elements</h6> <h6>Total blocked elements</h6>
<p class="text-danger text-right" id="statistics_value"></p> <p class="text-danger text-right" id="statistics_value"></p>
<h6>Percentage of total traffic</h6> <h6>Percentage of total traffic</h6>
<p class="text-right" id="statistics_value_global_percentage"></p> <p class="text-right" id="statistics_value_global_percentage"></p>
<p class="text-center"> <p class="text-center">
<button type="button" id="reset_counter_btn" class="btn btn-danger" title="Reset the global statistics">Reset counter</button> <button type="button" id="reset_counter_btn" class="btn btn-danger" title="Reset the global statistics">Reset counter</button>
</p> </p>
</div> </div>
</div> </div>
<footer> <footer>
<div class="row"> <div class="row">
<div class="col-sm-1 navbar-inverse text-center"> <div class="col-sm-1 navbar-inverse text-center">
<small class="text-muted">Version 1.1.2.1</small> <small class="text-muted">Version 1.1.2.2</small>
</div> </div>
</div> </div>
</footer> </footer>
</div> </div>
<!-- Optional JavaScript --> <!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS --> <!-- jQuery first, then Popper.js, then Bootstrap JS -->
@ -80,4 +80,4 @@
<script src="bootstrap.min.js"></script> <script src="bootstrap.min.js"></script>
<script src="popup_new.js"></script> <script src="popup_new.js"></script>
</body> </body>
</html> </html>

56
siteBlockedAlert.html Normal file
View File

@ -0,0 +1,56 @@
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="bootstrap.min.css">
<style>
body {
background-color: #333;
color: white;
text-shadow: 0 1px 3px rgba(0,0,0,.5);
}
h1 {
color: #f72d04;
}
.content {
position: absolute;
top: 40%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container content">
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<h1 class="text-center">This site or element was blocked by <b>ClearURLs</b> Add-on</h1>
<p class="text-center">
This site or element was blocked by <b>ClearURLs</b> Add-on, because we identify this site as an
advertising and/or tracking service.
<br>
To visit this site, you must temporarily deactivate the Add-on.
</p>
</div>
</div>
</div>
<footer class="navbar-fixed-bottom">
<div class="navbar-inverse text-center">
<small class="text-muted">Version 1.1.2.2</small>
</div>
</footer>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
</body>
</html>