123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- /*
- Author: Farooq Karimi Zadeh
- Email: fkz@riseup.net
- Repo: https://notabug.org/farooqkz/veil-client-ff
- */
- const BLUR = "blur(12px)";
- const SYNC_INTERVAL = 1 * 60 * 60;
- // ^ In seconds
- const isEmpty = async (obj) => {
- return await obj.then(function (d){
- return new Promise((a,b) => {a(Object.entries(d).length === 0 && d.constructor === Object);});
- });
- };
- const fetchList = function() {
- return fetch("http://de1.hashbang.sh:6101")
- .then(data => data.json())
- .then(data => {
- browser.storage.local.set({
- "badsites": data["sites"],
- "badpages": data["pages"],
- "veil_sync_time": (new Date()).getTime()
- });
- });
- };
- const blur_img = function(someimg) {
- someimg.style.filter = BLUR;
- };
- const unblur_img = function(someimg) {
- var new_filter = someimg.style.filter.replace(BLUR, "");
- someimg.style.filter = new_filter;
- };
- const blur_imgs = function() {
- for (let img of document.getElementsByTagName("IMG")){
- blur_img(img);
- }
- };
- browser.runtime.onMessage.addListener((data, sender) => {
- var elm = browser.menus.getTargetElement(data[0]);
- if (data[1] === "veilit") {
- blur_img(elm);
- } else if (data[1] === "unveilit") {
- unblur_img(elm);
- }
- });
- isEmpty(browser.storage.local.get("reported_sites")).then(empty => {
- if (empty){
- browser.storage.local.set({"reported_sites": []});
- } else {
- browser.storage.local.get("reported_sites").then(data => {
- let hostname = document.URL.split("/")[2];
- if (data["reported_sites"].includes(hostname)) {
- blur_imgs();
- }
- });
- }
- });
- isEmpty(browser.storage.local.get("reported_pages")).then(empty => {
- if (empty){
- browser.storage.local.set({"reported_pages": []});
- }else{
- browser.storage.local.get("reported_pages").then(data => {
- var url = document.URL;
- if (url.startsWith("https")) {
- url = url.replace("https", "http");
- }
- if (data["reported_pages"].includes(url))
- blur_imgs();
- });
- }
- });
- isEmpty(browser.storage.local.get(["badsites", "badpages"])).then(empty => {
- var f = url => {
- browser.storage.local.get("badsites").then(data => {
- let hostname = url.split("/")[2];
- let score = data["badsites"][hostname] || 0;
- if (score >= 10 || score === -1){
- blur_imgs();
- }
- });
- browser.storage.local.get("badpages").then(data => {
- url = url.startsWith("https")?url.replace("https", "http"):url;
- let score = data["badpages"][url] || 0;
- if (score >= 10 || score === -1){
- blur_imgs();
- }
- });
- }
- if (empty){
- fetchList().then(() => {f(document.URL);});
- }else{
- browser.storage.local.get("veil_sync_time").then(data => {
- if (((new Date()).getTime() - (data["veil_sync_time"] || 0)) >= SYNC_INTERVAL){
- // fetch a new list if the old list is from a day earlier or
- // more
- browser.storage.local.set({"veil_sync_time": (new Date()).getTime()});
- fetchList().then(() => {f(document.URL);});
- }else{
- f(document.URL);
- }
- });
- }
- });
|