Skip to content

Instantly share code, notes, and snippets.

@Fusion
Created July 4, 2015 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fusion/2413cbb889a6ae9f5191 to your computer and use it in GitHub Desktop.
Save Fusion/2413cbb889a6ae9f5191 to your computer and use it in GitHub Desktop.
In Workflowy, Ctrl+' turns urls into pictures
// ==UserScript==
// @name workflowy_txt2img.user.js
// @namespace http://voilaweb.com/
// @version 0.1
// @description Simply type a url and it will be converted to an image if it ends with gif/png/jpg
// @author Chris F Ravenscroft
// @homepage http://nexus.zteo.com
// @include http://workflowy.com/*
// @include https://workflowy.com/*
// @include http://*.workflowy.com/*
// @include https://*.workflowy.com/*
// @grant GM_addStyle
// ==/UserScript==
// CTRL+' shall be out shortcut
function CFR_main () {
function CFR_expandImgTags() {
var rootNode = document.querySelector('.mainTreeRoot');
var txtWalker = document.createTreeWalker (
rootNode,
NodeFilter.SHOW_TEXT,
{ acceptNode: function (node) {
if (node.nodeValue.trim() )
return NodeFilter.FILTER_ACCEPT;
return NodeFilter.FILTER_SKIP;
}
},
false
);
var deleteList = [];
var txtNode = null;
while (txtNode = txtWalker.nextNode () ) {
if(null != txtNode.nodeValue.match(/.+\.(jpg|png|gif|svg)/gi)) {
var newImgNode = document.createElement('img');
newImgNode.src = txtNode.nodeValue;
txtNode.parentNode.insertBefore(newImgNode, txtNode);
var newTxtNode = document.createElement('br');
txtNode.parentNode.insertBefore(newTxtNode, txtNode);
deleteList.push(txtNode);
}
}
/*
* Dead code for now! This almost works but when focusing/defocusing an
* item the picture and more importantly its source url may be lost.
* So, for now, this will be picture + br +url
for(var i=0; i<deleteList.length; i++) {
deleteList[i].parentNode.removeChild(deleteList[i]);
}
*/
}
document.addEventListener('keyup', function(event) {
switch(event.keyIdentifier) {
case 'U+0027':
if(event.ctrlKey) {
event.preventDefault();
CFR_expandImgTags();
}
break;
}
});
}
var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ CFR_main +')();'));
(document.body || document.head || document.documentElement).appendChild(script);
@Fusion
Copy link
Author

Fusion commented Jul 4, 2015

I could use listeners to update in real time, but in many cases it is more manageable/lightweight to only load images on demand.

@giffmex
Copy link

giffmex commented Oct 21, 2015

Wow! Where do I put that code to try it out???

Dave

@giffmex
Copy link

giffmex commented Oct 21, 2015

Installed it in Firefox via Greasemonkey, tried Ctrl+', and nothing happens. Advice?

@yantze
Copy link

yantze commented Apr 26, 2017

change the code:

    document.addEventListener('keyup', function(event) {
        switch(event.keyIdentifier) {
            case 'U+0027':
                if(event.ctrlKey) {
                    event.preventDefault();
                    CFR_expandImgTags();
                }
                break;
        }
    });

to

document.addEventListener('keyup', function(event) {
    switch(event.keyCode) {
        case 192:
            if (event.ctrlKey) {
                event.preventDefault();
                CFR_expandImgTags();
            }
            break;
    }
});

Press the keyboard Ctrl + `
Not the Ctrl + '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment