index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Author: Farooq Karimi Zadeh
  3. Email: fkz@riseup.net
  4. Repo: https://notabug.org/farooqkz/veil-client-ff
  5. */
  6. const BLUR = "blur(12px)";
  7. const SYNC_INTERVAL = 1 * 60 * 60;
  8. // ^ In seconds
  9. const isEmpty = async (obj) => {
  10. return await obj.then(function (d){
  11. return new Promise((a,b) => {a(Object.entries(d).length === 0 && d.constructor === Object);});
  12. });
  13. };
  14. const fetchList = function() {
  15. return fetch("http://de1.hashbang.sh:6101")
  16. .then(data => data.json())
  17. .then(data => {
  18. browser.storage.local.set({
  19. "badsites": data["sites"],
  20. "badpages": data["pages"],
  21. "veil_sync_time": (new Date()).getTime()
  22. });
  23. });
  24. };
  25. const blur_img = function(someimg) {
  26. someimg.style.filter = BLUR;
  27. };
  28. const unblur_img = function(someimg) {
  29. var new_filter = someimg.style.filter.replace(BLUR, "");
  30. someimg.style.filter = new_filter;
  31. };
  32. const blur_imgs = function() {
  33. for (let img of document.getElementsByTagName("IMG")){
  34. blur_img(img);
  35. }
  36. };
  37. browser.runtime.onMessage.addListener((data, sender) => {
  38. var elm = browser.menus.getTargetElement(data[0]);
  39. if (data[1] === "veilit") {
  40. blur_img(elm);
  41. } else if (data[1] === "unveilit") {
  42. unblur_img(elm);
  43. }
  44. });
  45. isEmpty(browser.storage.local.get("reported_sites")).then(empty => {
  46. if (empty){
  47. browser.storage.local.set({"reported_sites": []});
  48. } else {
  49. browser.storage.local.get("reported_sites").then(data => {
  50. let hostname = document.URL.split("/")[2];
  51. if (data["reported_sites"].includes(hostname)) {
  52. blur_imgs();
  53. }
  54. });
  55. }
  56. });
  57. isEmpty(browser.storage.local.get("reported_pages")).then(empty => {
  58. if (empty){
  59. browser.storage.local.set({"reported_pages": []});
  60. }else{
  61. browser.storage.local.get("reported_pages").then(data => {
  62. var url = document.URL;
  63. if (url.startsWith("https")) {
  64. url = url.replace("https", "http");
  65. }
  66. if (data["reported_pages"].includes(url))
  67. blur_imgs();
  68. });
  69. }
  70. });
  71. isEmpty(browser.storage.local.get(["badsites", "badpages"])).then(empty => {
  72. var f = url => {
  73. browser.storage.local.get("badsites").then(data => {
  74. let hostname = url.split("/")[2];
  75. let score = data["badsites"][hostname] || 0;
  76. if (score >= 10 || score === -1){
  77. blur_imgs();
  78. }
  79. });
  80. browser.storage.local.get("badpages").then(data => {
  81. url = url.startsWith("https")?url.replace("https", "http"):url;
  82. let score = data["badpages"][url] || 0;
  83. if (score >= 10 || score === -1){
  84. blur_imgs();
  85. }
  86. });
  87. }
  88. if (empty){
  89. fetchList().then(() => {f(document.URL);});
  90. }else{
  91. browser.storage.local.get("veil_sync_time").then(data => {
  92. if (((new Date()).getTime() - (data["veil_sync_time"] || 0)) >= SYNC_INTERVAL){
  93. // fetch a new list if the old list is from a day earlier or
  94. // more
  95. browser.storage.local.set({"veil_sync_time": (new Date()).getTime()});
  96. fetchList().then(() => {f(document.URL);});
  97. }else{
  98. f(document.URL);
  99. }
  100. });
  101. }
  102. });