DDG - Blocklist export/import

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

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


})();