DDG - Blocklist export/import

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

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.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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);


})();