Add offline storage for rules
This commit is contained in:
		
							parent
							
								
									a75daa0d28
								
							
						
					
					
						commit
						03b9d06d56
					
				
							
								
								
									
										60
									
								
								clearurls.js
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								clearurls.js
									
									
									
									
									
								
							| 
						 | 
					@ -52,24 +52,64 @@ function createProviders()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Convert the external data to JSON Objects and 
 | 
				
			||||||
 | 
					 * call the create provider function.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @param  {String} retrievedText - pure data form github
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function toJSON(retrievedText) {     
 | 
				
			||||||
 | 
					    data = JSON.parse(retrievedText);
 | 
				
			||||||
 | 
					    getKeys(data.providers);
 | 
				
			||||||
 | 
					    createProviders();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Load local saved data, if the browser is offline or
 | 
				
			||||||
 | 
					 * some other network trouble.
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					function loadOldDataFromStore()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    browser.storage.local.get('ClearURLsData', function(data){   
 | 
				
			||||||
 | 
					        if(data.ClearURLsData){
 | 
				
			||||||
 | 
					            data = data.ClearURLsData;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        else {
 | 
				
			||||||
 | 
					            data = "";
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        toJSON(data);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Fetch the Rules & Exception github.
 | 
					 * Fetch the Rules & Exception github.
 | 
				
			||||||
 * 
 | 
					 * 
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
function fetchFromURL()
 | 
					function fetchFromURL()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					 | 
				
			||||||
    fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true")
 | 
					    fetch("https://raw.githubusercontent.com/KevinRoebert/ClearUrls/master/data/data.json?flush_cache=true")
 | 
				
			||||||
    .then((response) => response.text().then(toJSON));
 | 
					    .then(checkResponse)
 | 
				
			||||||
 | 
					    .catch(function(error){
 | 
				
			||||||
 | 
					        loadOldDataFromStore();
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    function toJSON(retrievedText) { 
 | 
					    function checkResponse(response)
 | 
				
			||||||
        data = JSON.parse(retrievedText);
 | 
					    {
 | 
				
			||||||
        getKeys(data.providers);
 | 
					        var responseText = response.clone().text().then(function(responseText){
 | 
				
			||||||
        createProviders();
 | 
					            if(response.ok)
 | 
				
			||||||
 | 
					            {                
 | 
				
			||||||
 | 
					                browser.storage.local.set({"ClearURLsData": responseText});
 | 
				
			||||||
 | 
					                toJSON(responseText);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					            else {
 | 
				
			||||||
 | 
					                 loadOldDataFromStore();           
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
//Execute the command
 | 
					
 | 
				
			||||||
fetchFromURL();
 | 
					 | 
				
			||||||
// ##################################################################
 | 
					// ##################################################################
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
| 
						 | 
					@ -368,6 +408,10 @@ function setBadgedStatus() {
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Call the fetch, counter and status functions
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					fetchFromURL();
 | 
				
			||||||
setBadgedStatus();
 | 
					setBadgedStatus();
 | 
				
			||||||
setGlobalCounter();
 | 
					setGlobalCounter();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,8 +1,8 @@
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  "manifest_version": 2,
 | 
					  "manifest_version": 2,
 | 
				
			||||||
  "name": "ClearURLs",
 | 
					  "name": "ClearURLs",
 | 
				
			||||||
  "version": "1.1.1.4",
 | 
					  "version": "1.1.2.0",
 | 
				
			||||||
  "author": "Kevin R., Arne S.",
 | 
					  "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",
 | 
				
			||||||
  "icons": {
 | 
					  "icons": {
 | 
				
			||||||
| 
						 | 
					@ -18,7 +18,7 @@
 | 
				
			||||||
      "38": "icon38.png"
 | 
					      "38": "icon38.png"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "default_title": "ClearURLs Add-on",
 | 
					    "default_title": "ClearURLs Add-on",
 | 
				
			||||||
    "default_popup": "popup.html"
 | 
					    "default_popup": "popup_new.html"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "permissions": [
 | 
					  "permissions": [
 | 
				
			||||||
    "*://*/*",
 | 
					    "*://*/*",
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user