garnierjm (owner)

Revisions

gist: 208244 Download_button fork
public
Public Clone URL: git://gist.github.com/208244.git
Embed All Files: show embed
x.js #
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Mozilla Ubiquity Command for Diigo, currently only support add bookmark.
* Some code from https://ubiquity.mozilla.com/standard-feeds/social.js
*/
 
var store = Application.storage;
const DIIGO_CUR_LOGIN = "ubiquity_diigo_cur_login";
var Choices = {"yes": "yes", "no": "no"};
 
var noun_type_share = {
  _name: "yes/no",
  suggest: function(text, html) {
    var suggestions = [];
    for ( var word in Choices ) {
      if ( word.indexOf(text.toLowerCase()) > -1 ) {
        var sugg = CmdUtils.makeSugg(word, word, Choices[word]);
        suggestions.push(sugg);
      }
    }
    return suggestions;
  }
};
 
function setCurLogin(login) {
  store.set(DIIGO_CUR_LOGIN, login);
}
 
function getCurLogin() {
    var login = store.get(DIIGO_CUR_LOGIN, '');
    if(!login){
      var logins = getDiigoIds();
    if(logins.length > 0){
      login = logins[0];
      setCurLogin(login);
      }
    }
  return login;
}
 
function make_basic_auth(user, password){
  var tok = user + ":" + password;
  var hash = CmdUtils.getWindow().btoa(tok); //Base64 encode.
  return "Basic " + hash;
}
 
function getDiigoIds () {
  var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
  var logins = passwordManager.findLogins({}, "https://secure.diigo.com", "", null);
  return logins;
}
 
function split_tags(tags) {
  if (tags.search(/,/) < 0) {
    return tags.replace(/ /g, ', ');
  }
  return tags;
}
 
CmdUtils.CreateCommand({
  name: ["Diigo", "diigo"],
  homepage: "blog.wangbin1979.com",
  author: { name: "Wang Bin", email: "wangbin.zibo@gmail.com" },
  version: "0.5",
  license: "MPL,GPL",
  description: "bookmark the page to your Diigo account",
  icon: "http://www.diigo.com/favicon.ico",
  arguments: [
    {role: 'object', nountype: noun_arb_text, label: 'notes'},
    {role: 'instrument', nountype: noun_arb_text, label: 'tags'},
    {role: 'alias', nountype: noun_type_share, label: 'share'}
  ],
 
  preview: function(pblock, args) {
    var doc = CmdUtils.getDocument();
    args.object.text = doc.getSelection() ? doc.getSelection().substring(0,140) : args.object.text;
    args.instrument.text = split_tags(args.instrument.text);
    var curLogin = getCurLogin();
 
    var previewTemplate = [
      '<style type="text/css">',
        '.preview a { color: #3774D0 }',
        '.preview dl {margin: 0; padding: 0; }',
        '.preview dd {float: left; text-align: right; width: 50px; margin: 0; padding: 0; }',
        '.preview dt {margin-left: 60px;}',
      '</style>',
      '<div class="preview">',
        '<p>Bookmark to <img src="http://www.diigo.com/favicon.ico" /> <a href="http://www.diigo.com/user/${user}">Diigo</a>: </p>',
        '<dl>',
          '<dd>Title:</dd><dt>${title}</dt>',
          '<dd>Url:</dd><dt>${url}</dt>',
          args.object.text ? '<dd>Notes:</dd><dt>${desc}</dt>':' ',
          args.instrument.text ? '<dd>Tags:</dd><dt>${tags}</dt>':' ',
          args.alias.text ? '<dd>Share:</dd><dt>${shared}</dt>': ' ',
        '</dl>',
      '</div>'].join("\n");
    var previewData = {
      user: curLogin ? curLogin.username: '',
      title: doc.title,
      url: doc.URL,
      tags: args.instrument.text,
      desc: args.object.text,
      shared: args.alias.text
    };
    var previewHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
    pblock.innerHTML = previewHTML;
  },
  execute: function(args) {
    var bookmark_url = "http://api2.diigo.com/bookmarks";
    var doc = CmdUtils.getDocument();
    args.instrument.text = split_tags(args.instrument.text);
    var bookmark_params = {
      title: doc.title,
      url: doc.URL,
      tags: args.instrument.text,
      desc: args.object.text || "",
      shared: args.alias.text
    };
 
    var curLogin = getCurLogin();
    if( curLogin ){
      var auth = make_basic_auth(curLogin.username, curLogin.password);
    } else {
      var auth = null;
    }
    jQuery.ajax({
      type: "POST",
      url: bookmark_url,
      data: bookmark_params,
      dataType: "json",
      beforeSend: function(req) {
        if( auth ) req.setRequestHeader("Authorization", auth);
      },
      error: function() {
        displayMessage("Diigo error - bookmark failure.");
      },
      success: function(resp) {
        displayMessage(resp.message);
      }
    });
  }
});