166
edits
Line 427: | Line 427: | ||
Das "translate" Kommando ist bestens dafür geeignet, mehr über Modifizierer und den Substantiv-Typ <code>noun_type_language</code> zu lernen. | Das "translate" Kommando ist bestens dafür geeignet, mehr über Modifizierer und den Substantiv-Typ <code>noun_type_language</code> zu lernen. | ||
= Twitter: Putting It All Together = | |||
We've now covered everything we need to cover in order to write a command that allows us to [http://en.wikipedia.org/wiki/Twitter Twitter] from Ubiquity. Many thanks to [http://theunfocused.net/ Blair McBride] for writing this command. This is a fully functioning command: the browser takes care of the odd edge cases, like not being logged in. | |||
<pre> | |||
// max of 140 chars is recommended, but it really allows 160 | |||
const TWITTER_STATUS_MAXLEN = 160; | |||
CmdUtils.CreateCommand({ | |||
name: "twitter", | |||
takes: {status: noun_arb_text}, | |||
homepage: "http://theunfocused.net/moz/ubiquity/verbs/", | |||
author: {name: "Blair McBride", homepage: "http://theunfocused.net/"}, | |||
license: "MPL", | |||
preview: function(previewBlock, statusText) { | |||
var previewTemplate = "Updates your Twitter status to: <br/>" + | |||
"<b>${status}</b><br /><br />" + | |||
"Characters remaining: <b>${chars}</b>"; | |||
var truncateTemplate = "<br />The last <b>${truncate}</b> " + | |||
"characters will be truncated!"; | |||
var previewData = { | |||
status: statusText.text, | |||
chars: TWITTER_STATUS_MAXLEN - statusText.text.length | |||
}; | |||
var previewHTML = CmdUtils.renderTemplate(previewTemplate, | |||
previewData); | |||
if(previewData.chars < 0) { | |||
var truncateData = { | |||
truncate: 0 - previewData.chars | |||
}; | |||
previewHTML += CmdUtils.renderTemplate(truncateTemplate, | |||
truncateData); | |||
} | |||
previewBlock.innerHTML = previewHTML; | |||
}, | |||
execute: function(statusText) { | |||
if(statusText.text.length < 1) { | |||
displayMessage("Twitter requires a status to be entered"); | |||
return; | |||
} | |||
var updateUrl = "https://twitter.com/statuses/update.json"; | |||
var updateParams = { | |||
source: "ubiquity", | |||
status: statusText.text | |||
}; | |||
jQuery.ajax({ | |||
type: "POST", | |||
url: updateUrl, | |||
data: updateParams, | |||
dataType: "json", | |||
error: function() { | |||
displayMessage("Twitter error - status not updated"); | |||
}, | |||
success: function() { | |||
displayMessage("Twitter status updated"); | |||
} | |||
}); | |||
} | |||
}); | |||
</pre> |
edits