Redacted - roamresearch.com

adding a tag of #redacted to a block will cause it to not be able to be read unless clicked on

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Redacted - roamresearch.com
// @namespace   https://ryanguill.com
// @match       https://roamresearch.com/#/app
// @grant       none
// @version     0.1
// @author      Ryan Guill
// @description adding a tag of #redacted to a block will cause it to not be able to be read unless clicked on
// @run-at      document-idle
// ==/UserScript==

const debug_enabled = true;

const redacted_blocks_enabled = true;

function setup_styles () {
  const style = document.createElement('style');
  document.head.appendChild(style);
  style.id = 'roam-custom-js-styles';

  
  style.sheet.insertRule(`
    div.roam-block-container[data-redacted] {
      background-color: #000;
      color: #000;
    }
  `);
  style.sheet.insertRule(`
    div.roam-block-container[data-redacted] textarea {
      background-color: #FFF;
    }
  `);
}



function redacted_blocks () {
  const blocks = document.querySelectorAll("div[data-redacted]");

  for (const block of blocks) {
    block.removeAttribute("data-redacted");
  }

  const tags = [...document.querySelectorAll("span[data-tag]")]
    .filter((element) => element.dataset.tag !== undefined && element.dataset.tag.toLowerCase() === "redacted");

  for (const tag of tags) {
      //on the tag itself set a color
      tag.dataset.redacted = true;
      tag.style.display = "none";

      const div = tag.closest("div.roam-block-container");
      
      div.dataset.redacted = true;
    }
    
  return true;
}

function call_with_timer (f, label, debug_enabled) {
  if (debug_enabled) {
    console.time(label);
  }
  f();
  if (debug_enabled) {
    console.timeEnd(label);
  }
}

function run_scripts () {
  setTimeout(function () {
    call_with_timer(function () {
      if (redacted_blocks_enabled) {
        call_with_timer(redacted_blocks, 'redacted_blocks', debug_enabled);
      }
    }, "Total: run_scripts", debug_enabled);
  }, 100)
}



// Wait for Roam to load before initializing
const observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
        const newNodes = mutation.addedNodes // DOM NodeList
        if (newNodes !== null) { // If there are new nodes added
          for (let node of newNodes) {
            
              if (node.classList.contains('roam-body')) { // body has loaded
                  console.log('Observer finished.')
                  observer.disconnect()
                  init()
              }
          }
        }
    })
})

const config = {
    attributes: true,
    childList: true,
    characterData: true
}

const target = document.getElementById('app');
if (target) {
  observer.observe(target, config)
  console.log('Observer running...')
}

function init () {
  
    setup_styles();
  
    //for the first 60 seconds, run the scripts every 3 seconds to load initially
    for (let time = 0; time <= 60000; time += 3000) {
      setTimeout(function () {
        if (debug_enabled) {
          console.log("timeout", time);
        }
        run_scripts();
      }, time);
    }

    //after that, run the scripts every 15 seconds indefinately
    setInterval(function (){
        if (debug_enabled) {
          console.log("interval");
        } 
      run_scripts();
    }, 15000)
    
    /*
    document.addEventListener("blur", function () {
      //console.log("blur");
      runScripts()
    }, true);
    */
    /*
    document.addEventListener("change", function () {
      //console.log("change");
      runScripts()
    }, true);
    */
    document.addEventListener("focus", function () {
      if (debug_enabled) {
          console.log("focus");
      }
      run_scripts()
    }, true);
    
    //disable cmd-s
    document.addEventListener("keydown", function(e) {
      if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
        e.preventDefault()
      }
    }, false)
  

}