DDG - Blocklist export/import

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);


})();