(function() {

  function reapply(arr) {
    for (let i = 0, len = arr.length; i < len; i++) {
      switch (arr[i].type) {
        case 32: // string
          Services.prefs.setCharPref(arr[i].name, arr[i].value);
          break;
        case 64: // int
          Services.prefs.setIntPref(arr[i].name, arr[i].value);
          break;
        case 128: // boolean
          Services.prefs.setBoolPref(arr[i].name, arr[i].value);
          break;
        default:
          console.log("error re-appyling value for '"+arr[i].name+"' !"); // should never happen
      }
    }
  }

  function myreset(arr) {
    for (let i = 0, len = arr.length; i < len; i++) {
      Services.prefs.clearUserPref(arr[i].name);
    }
  }

  let ops = [
    'accessibility.force_disabled',
    'browser.cache.offline.enable',
    'browser.display.use_document_fonts',
    'browser.formfill.enable',
    'browser.link.open_newwindow.restriction',
    'browser.search.suggest.enabled',
    'browser.storageManager.enabled',
    'browser.tabs.remote.allowLinkedWebInFileUriProcess',
    'browser.urlbar.autoFill',
    'browser.urlbar.autoFill.typed',
    'browser.urlbar.oneOffSearches',
    'browser.urlbar.suggest.searches',
    'camera.control.face_detection.enabled',
    'canvas.capturestream.enabled',
    'dom.caches.enabled',
    'dom.event.clipboardevents.enabled',
    'dom.idle-observers-api.enabled',
    'dom.IntersectionObserver.enabled',
    'dom.popup_allowed_events',
    'dom.popup_maximum',
    'dom.push.connection.enabled',
    'dom.push.enabled',
    'dom.push.serverURL',
    'dom.serviceWorkers.enabled',
    'dom.storageManager.enabled',
    'dom.vibrator.enabled',
    'dom.webaudio.enabled',
    'dom.webnotifications.enabled',
    'dom.webnotifications.serviceworker.enabled',
    'font.blacklist.underline_offset',
    'full-screen-api.enabled',
    'geo.wifi.uri',
    'gfx.downloadable_fonts.woff2.enabled',
    'gfx.font_rendering.graphite.enabled',
    'gfx.font_rendering.opentype_svg.enabled',
    'intl.accept_languages',
    'javascript.options.asmjs',
    'javascript.options.wasm',
    'keyword.enabled',
    'layout.css.font-loading-api.enabled',
    'layout.css.visited_links_enabled',
    'mathml.disabled',
    'media.autoplay.enabled',
    'media.getusermedia.screensharing.allowed_domains',
    'media.getusermedia.screensharing.enabled',
    'media.ondevicechange.enabled',
    'network.auth.subresource-img-cross-origin-http-auth-allow',
    'network.cookie.thirdparty.sessionOnly',
    'network.http.altsvc.enabled',
    'network.http.altsvc.oe',
    'network.http.redirection-limit',
    'network.http.referer.XOriginPolicy',
    'network.protocol-handler.external.ms-windows-store',
    'permissions.manager.defaultsUrl',
    'plugin.default.state',
    'plugin.defaultXpi.state',
    'plugin.scan.plid.all',
    'plugin.sessionPermissionNow.intervalInMinutes',
    'privacy.trackingprotection.enabled',
    'security.cert_pinning.enforcement_level',
    'security.csp.experimentalEnabled',
    'security.data_uri.block_toplevel_data_uri_navigations',
    'security.family_safety.mode',
    'security.mixed_content.block_display_content',
    'security.mixed_content.use_hsts',
    'security.OCSP.require',
    'security.pki.sha1_enforcement_level',
    'security.ssl.treat_unsafe_negotiation_as_broken',
    'security.tls.enable_0rtt_data',
    'security.tls.version.max',
    'security.tls.version.min',
    'security.xpconnect.plugin.unrestricted',
    'signon.autofillForms',
    'signon.formlessCapture.enabled',
    'webchannel.allowObject.urlWhitelist',

    /* known culprits */
    'dom.workers.enabled',
    'network.cookie.cookieBehavior',
    'privacy.firstparty.isolate',
    'privacy.resistFingerprinting',

    'last.one.without.comma'
  ]


  if("undefined" === typeof(Services)) {
    alert("about:config needs to be the active tab!");
    return;
  }

  let aBACKUP = [];
  let dummy = 0;
  for (let i = 0, len = ops.length; i < len; i++) {
    if (Services.prefs.prefHasUserValue(ops[i])) {
      dummy = Services.prefs.getPrefType(ops[i]);
      switch (dummy) {
        case 32: // string (see https://dxr.mozilla.org/mozilla-central/source/modules/libpref/nsIPrefBranch.idl#31)
          dummy = Services.prefs.getCharPref(ops[i]);
          aBACKUP.push({'name':ops[i],'value': dummy,'type':32});
          break;
        case 64: // int
          dummy = Services.prefs.getIntPref(ops[i]);
          aBACKUP.push({'name':ops[i],'value': dummy,'type':64});
          break;
        case 128: // boolean
          dummy = Services.prefs.getBoolPref(ops[i]);
          aBACKUP.push({'name':ops[i],'value': dummy,'type':128});
          break;
        default:
          console.log("error detecting pref-type for '"+ops[i]+"' !");
      }
    }
  }
  // console.log(aBACKUP.length, "user-set prefs from our list detected and value stored.");


  myreset(aBACKUP); // resetting all detected prefs

  let myArr = aBACKUP

  focus();
  if (confirm("all detected prefs reset.\n\n!! KEEP THIS PROMPT OPEN AND TEST THE SITE IN ANOTHER TAB !!\n\nIF the problem still exists, this script can't help you - click cancel to re-apply your values and exit.\n\nClick OK if your problem is fixed.")) {
    reapply(aBACKUP);
    myreset(myArr.slice(0, parseInt(myArr.length/2)));
    while (myArr.length >= 2) {
      alert("NOW TEST AGAIN !");
      if (confirm("if the problem still exists click OK, otherwise click cancel.")) {
        myArr = myArr.slice(parseInt(myArr.length/2));
      } else {
        myArr = myArr.slice(0, parseInt(myArr.length/2));
      }
      reapply(aBACKUP);
      myreset(myArr.slice(0, parseInt(myArr.length/2))); // reset half of the remaining prefs
    }
  }

  reapply(aBACKUP); // re-apply all values

  let output = "";
  for (let i = 0, len = myArr.length; i < len; i++) {
    output = output + myArr[i].name + "\n";
  }
  alert("narrowed it down to:\n\n"+output);

  myreset(myArr); // reset the culprit

})();