Action

Decode...

Posted by agiletortoise, Last update 6 months ago

Decode selected text with options. Choose to URL decode, unescape HTML entities, Base64 or ROT13 decoding the string.

The result can replace the current selection, be placed in the clipboard, or opened as a new draft.

Steps

  • script

    //rot13 methods
    
    const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
    const rot13Alphabet = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm".split("");
    
    function rot13(str) {
    	let index     = x => alphabet.indexOf(x);
    	let translate = x => index(x) > -1 ? rot13Alphabet[index(x)] : x;
      	return str.split('').map(translate).join('');
    }
    
    
    function rot13Decrypt(s) {
    	let result = "";
    	for (let x=0; x<s.length; x++) {
    		for (let y=0; y<alphabet.length; y++) {
    			if (s[x]==alphabet[y]) {
    				result+=rot13Alphabet[y];
    			}
    		}
    		if(s[x]==" ") {
    			result+=" ";
    		}
    	}
    	return result;
    }
    
  • script

    // prompt for options and run
    let f = () => {
    	let p = Prompt.create();
    	
    	p.title = "Decode Selection";
    	p.message = "Decode the text selection";
    		
    	p.addSelect("op", "Type", ["URL Decode", "Unescape HTML Entities", "Base64 Decode", "ROT13 Decode"], ["URL Decode"], false);
    	
    	p.addSelect("output", "Output", ["Replace selection", "Clipboard", "New draft"], ["Replace selection"], false);
    	
    	p.addButton("Decode");
    	
    	if (!p.show()) {
    		return false;
    	}
    	
    	let selection = editor.getSelectedText();
    
    	const op = p.fieldValues["op"];
    	if (op == "URL Decode") {
    		selection = decodeURIComponent(selection);
    	}
    	else if (op == "Unescape HTML Entities") {
    		selection = HTML.unescape(selection);
    	}
    	else if (op == "Base64 Decode") {
    		selection = Base64.decode(selection);
    	}
    	else if (op == "ROT13 Decode") {
    		selection = rot13Decrypt(selection);
    	}
    	
    	const output = p.fieldValues["output"][0];
    	switch(output) {
    		case "Replace selection":
    			editor.setSelectedText(selection);
    			break;
    		case "Clipboard":
    			app.setClipboard(selection);
    			break;
    		case "New draft":
    			let d = Draft.create();
    			d.content = selection;
    			d.update();
    			editor.load(d);
    			break;
    	}
    
    	return true;
    }
    
    if (!f()) {
    	context.cancel();
    }
    

Options

  • After Success Default
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.