Skip to content

Instantly share code, notes, and snippets.

@Calvein
Created July 30, 2012 19:16
Show Gist options
  • Save Calvein/3209303 to your computer and use it in GitHub Desktop.
Save Calvein/3209303 to your computer and use it in GitHub Desktop.
The beginning of a Chrome Extension to add markdown on http://twitter.com
/*
@TODO :
- Add italic-bold
- Change the .tweet-counter value
*/
!function(window, document) {
// From http://mothereff.in/twitalics
var types = {
// `new Number` is needed since we want to add properties to the numbers later (cache)
'italic': new Number(0xddc1)
, 'bold': new Number(0xdd8d)
, 'italic-bold': new Number(0xddf5)
}
function replace(string, lowercaseCode, type) {
return string.replace(/[a-zA-Z]/g, function(character) {
var charCode = character.charCodeAt()
, isUppercase = charCode < 97
, typeRef = types[type]
return typeRef[character] || (typeRef[character] = String.fromCharCode(0xd835, charCode + lowercaseCode + (isUppercase ? 6 : 0)))
})
}
/*
doc.addEventListener('input', function(e) {
var el = e.target
if (el.classList.contains('twitter-anywhere-tweet-box-editor')) {
var count = el.value.length
el.value = el.value.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, function(match, osef, word) {
count -= 4
}).replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, function(match, osef, word) {
count -= 2
})
console.log(count)
}
})
*/
// The click on the button
doc.addEventListener('click', function(e) {
var el = e.target
if (el.classList.contains('tweet-button')) {
var textarea = el.parentElement.parentElement.parentElement.querySelector('.twitter-anywhere-tweet-box-editor')
textarea.value = textarea.value.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, function(match, osef, word) {
return replace(word, types.bold, 'bold')
}).replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, function(match, osef, word) {
return replace(word, types.italic, 'italic')
})
}
}, true)
}(this, doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment