Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Workflowy Strikethrough script
// ==UserScript==
// @name Workflowy Strikethrough script
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Adds a style to the page for Strikethrough text, and sets any items with the tag #st to use that style
// @author Michael Durland
// @match https://workflowy.com/*
// @grant none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// ==/UserScript==
(function() {
'use strict';
var j = jQuery.noConflict();
// Add a style to the document for strikethrough formatting.
var class_strikethrough = '' +
'.strikethrough { ' +
'color: #ccc !important; ' +
'text-decoration: line-through !important; ' +
'}';
j("<style>")
.prop("type", "text/css")
.html(class_strikethrough)
.appendTo("head");
// Define a function to add the strikethrough formatting class to all parents of nodes with the #st tag.
function update_strikethroughs() {
// Need to remove first in case the #st tag was removed.
j('.strikethrough').removeClass("strikethrough");
// Need to add the class to the parent of the #st tag so that the entire item gets the strikethrough formatting.
j('.contentTag[title="Filter #st"]').parents("div.content").addClass("strikethrough");
};
// Set a periodic timer to update the styles.
setInterval(update_strikethroughs, 200);
// Setup a keyboard shortcut of "Alt -" to toggle strikethrough on the current node.
window.addEventListener('keydown', function(e) {
let is_minus_key = (e.key == '-');
if (is_minus_key && e.altKey) {
WF.save(); // need to save first. Otherwise the API might return stale data.
var current_item = WF.focusedItem();
if (current_item) {
var item_text = current_item.getName();
if (item_text) {
let is_strikethrough = item_text.includes("#st");
if (is_strikethrough) {
// Remove the tag.
item_text = item_text.replace("#st", "").trim();
}
else {
// Add the tag.
item_text = item_text + " #st";
}
// Strip off whitespace. This is needed if the #st tag is removed.
// Otherwise the string is left with a leading space if the tag was at
// the front of the string, or a trailing space if the tag was at
// the end of the string.
WF.setItemName(current_item, item_text);
}
}
}
}, true);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment