Welcome, guest | Sign In | My Account | Store | Cart

This is an updated version of another recipe (#577170) to post selected text to pastebin.com. This version uses the latest API. You'll want to change the following variables to your preferences: email, nick, expiry.

JavaScript, 98 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var pastebin = {
    post : function() {
        var selection = ko.views.manager.currentView.selection;
        
        if (selection == "") {
            ko.dialogs.alert("No selection found");
            return;
        }
        
        var lang = encodeURIComponent(this.ko2pastebinLanguage());
        var text = encodeURIComponent(selection);
        var email = encodeURIComponent("me@mail.com");
        var nick = encodeURIComponent("me");
        var expiry = encodeURIComponent("1D"); // N = Never, 10M = 10 Minutes, 1H = 1 Hour, 1D = 1 Day, 1M = 1 Month
        
        var httpReq = new XMLHttpRequest();
        httpReq.open("post", "http://pastebin.com/api_public.php", false);
        httpReq.setRequestHeader("content-type",
                                 "application/x-www-form-urlencoded");

        var requestString = "paste_private=1&paste_format={1}&paste_code={2}&paste_email={3}&paste_name={4}&paste_expire_date={5}";
        var sendString = requestString
                            .replace("{1}", lang)
                            .replace("{2}", text)
                            .replace("{3}", email)
                            .replace("{4}", nick)
                            .replace("{5}", expiry);
        httpReq.setRequestHeader("Content-length", sendString.length);
        httpReq.send(sendString);

        var url = this.getReturnURL(httpReq);
        httpReq.setRequestHeader("Connection", "close");
        this.copyText(url);
        ko.statusBar.AddMessage("Url " + url
                                + " copied on clipboard using lang " + lang,
                                "pastebin_macro", 3000, true)
        ko.browse.openUrlInDefaultBrowser(url);
    },
    
    getReturnURL : function(httpReq) {
        return httpReq.responseText;
    },
    
    copyText : function(str) {
        Components.classes["@mozilla.org/widget/clipboardhelper;1"]
            .getService(Components.interfaces.nsIClipboardHelper)
            .copyString(str); 
    },
    
    ko2pastebinLanguage : function() {
        var langMap = {};

        langMap["Text"] = "text";
        langMap["ActionScript"] = "actionscript";
        langMap["Ada"] = "ada";
        langMap["Apache"] = "apache";
        langMap["Assembler"] = "asm";
        langMap["Bash"] = "bash";
        langMap["C++"] = "c";
        langMap["C++"] = "cpp";
        langMap["CSS"] = "css";
        langMap["Diff"] = "diff";
        langMap["Eiffel"] = "eiffel";
        langMap["Fortran"] = "fortran";
        langMap["FreeBasic"] = "freebasic";
        langMap["HTML"] = "html4strict";
        langMap["Java"] = "java";
        langMap["JavaScript"] = "javascript";
        langMap["Lisp"] = "lisp";
        langMap["Lua"] = "lua";
        langMap["Matlab"] = "matlab";
        langMap["SQL"] = "mysql";
        langMap["Nsis"] = "nsis";
        langMap["PL-SQL"] = "oracle8";
        langMap["Pascal"] = "pascal";
        langMap["Perl"] = "perl";
        langMap["PHP"] = "php";
        langMap["Python"] = "python";
        langMap["Ruby"] = "ruby";
        langMap["Scheme"] = "scheme";
        langMap["Smarty"] = "smarty";
        langMap["SQL"] = "sql";
        langMap["Tcl"] = "tcl";
        langMap["VisualBasic"] = "vb";
        langMap["XBL"] = "xml";
        langMap["XML"] = "xml";
        langMap["XSLT"] = "xml";
        langMap["XUL"] = "xml";

        language = langMap[ko.views.manager.currentView.document.language];
        if (language == undefined) {
            return "text";
        }
        return language;
    }

};
pastebin.post();
Created by Nathan Rambeck on Mon, 6 Dec 2010 (MIT)
JavaScript recipes (69)
Nathan Rambeck's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks