DDG - Blocklist export/import

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

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 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);


})();