Action

Encode...

Posted by agiletortoise, Last update 6 months ago

Encode selected text with options. Choose to URL encode, escape HTML entities, Base64 or ROT13 encode 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 = "Encode Selection";
    	p.message = "Encode the text selection";
    		
    	p.addSelect("op", "Type", ["URL Encode", "Escape HTML Entities", "Base64 Encode", "ROT13 Encode"], ["URL Encode"], false);
    	
    	p.addSelect("output", "Output", ["Replace selection", "Clipboard", "New draft"], ["Replace selection"], false);
    	
    	p.addButton("Encode");
    	
    	if (!p.show()) {
    		return false;
    	}
    	
    	let selection = editor.getSelectedText();
    
    	const op = p.fieldValues["op"];
    	if (op == "URL Encode") {
    		selection = encodeURIComponent(selection);
    	}
    	else if (op == "Escape HTML Entities") {
    		selection = HTML.escape(selection);
    	}
    	else if (op == "Base64 Encode") {
    		selection = Base64.encode(selection);
    	}
    	else if (op == "ROT13 Encode") {
    		selection = rot13(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.