DDG - Blocklist export/import

Allows the user to export and import DDG blocklist to/from a local file

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         DDG - Blocklist export/import
// @namespace    https://github.com/Procyon-b
// @version      0.2
// @description  Allows the user to export and import DDG blocklist to/from a local file
// @author       Achernar
// @match        https://duckduckgo.com/settings*
// @match        https://noai.duckduckgo.com/settings*
// @match        https://start.duckduckgo.com/settings*
// @match        https://safe.duckduckgo.com/settings*
// @run-at       document-end
// @grant        none
// ==/UserScript==

(function() {
"use strict";
var BL, done;

function findBL() {
  if (done) return;
  BL=document.querySelector('.site-exclusion-header');
  whenBL();
  }

function whenBL() {
  if (!BL) return;
  done=1;
  var pn=BL.parentNode, r=pn.parentNode;
  var sv=document.createElement('span');
  sv.innerHTML='Export blocklist';
  sv.className='btn';
  sv.onclick=exportBL;
  var imp=document.createElement('span');
  imp.innerHTML='Import blocklist';
  imp.className='btn';
  imp.onclick=importBL;
  pn.appendChild(document.createElement('br'));
  pn.appendChild(sv);
  pn.appendChild(document.createTextNode('   '));
  pn.appendChild(imp);
  }

function importBL() {
  var DS=window.localStorage.getItem('duckduckgo_settings');
  try {
    DS=JSON.parse(DS) || {}; // default settings. No LS
    let F=document.createElement('input');
    F.type='file';
    F.accept='application/json';
    let L=document.createElement('button');
    L.onclick=function(){F.click();};
    L.click();

    F.onchange=function(){
      let file=F.files[0];
      if (!file) return;
      let reader=new FileReader();
      reader.onload=function(){
        try{
          let nKbm=JSON.parse(reader.result);
          if (!nKbm.value || (Object.keys(nKbm).length != 2) ) return; // wrong file
          let kbm=DS.kbm || {value:''};
          let L={};
          kbm.value.split(',').forEach( function(n){if (n) L[n]=1;} );
          nKbm.value.split(',').forEach( function(n){if (n) L[n]=1;} );
          nKbm.value=Object.keys(L).join(',');
          DS.kbm=nKbm;
          DS=JSON.stringify(DS);
          window.localStorage.setItem('duckduckgo_settings', DS);
          location.reload(false);
        }catch(e){}

        }
      reader.readAsText(file);
      }

  }catch(e){}
  ;}

function exportBL() {
  var DS=window.localStorage.getItem('duckduckgo_settings');
  try {
    DS=JSON.parse(DS);
    if (DS.kbm) {
      let t=DS.kbm;
      t=JSON.stringify(t);
      let blob=new Blob([t], { type: 'text/plain' });
      let url=URL.createObjectURL(blob);
      let a=document.createElement('a');
      a.href=url;
      a.download='DDG-blocklist.'+(new Date().toISOString())+'.json';
      a.click();
      URL.revokeObjectURL(url);
      }
  }catch(e){}
  }

findBL();
if (!BL) document.body.onload=findBL;

function tryUntil(F, TO=150, c=-1, fail) {
  if (!c--) {
    fail && fail();
    return;
    }
  try{F();}catch(e){setTimeout(function(){tryUntil(F,TO,c,fail);}, TO)}
  }

tryUntil( function(){
  findBL();
  if (!BL) throw new Error('');
  }, null, 50);


})();