Skip to content

Instantly share code, notes, and snippets.

@ucnv
Created May 16, 2010 13:55
Show Gist options
  • Save ucnv/402893 to your computer and use it in GitHub Desktop.
Save ucnv/402893 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Twitter + Yahoo Placemaker
// @namespace http://aspietribe.com/
// @description Automatically adds a geo location to your tweet.
// @include http://twitter.com/
// ==/UserScript==
var appidE = 'your-appid-for-wherein.yahooapis.com';
document.getElementById('tweeting_button').parentNode.addEventListener('click', function(e) {
post();
e.preventDefault();
e.stopPropagation();
}, true);
function post() {
var tweet = document.getElementById('status').value;
var query = [], params = {
appid: appidE,
inputLanguage: 'ja-JP',
documentType: 'text/plain',
documentContent: tweet,
};
for(var i in params) query.push(i + '=' + encodeURIComponent(params[i]));
var url = 'http://wherein.yahooapis.com/v1/document';
GM_xmlhttpRequest({
method : 'POST',
url: url,
data: query.join('&'),
headers: {"Content-Type": "application/x-www-form-urlencoded"},
onload: function(res) {
var lat, lng;
var x = new DOMParser().parseFromString(res.responseText, 'text/xml');
var e = x.getElementsByTagName('place') || x.getElementsByTagName('geographicScope');
if(e && e.length) {
lat = e[0].getElementsByTagName('latitude')[0].textContent;
lng = e[0].getElementsByTagName('longitude')[0].textContent;
}
if(lat && lng) {
document.getElementById('lat').value = lat;
document.getElementById('lon').value = lng;
document.getElementById('display_coordinates').value = true;
}
var form = document.getElementById('status_update_form');
var evt = document.createEvent('HTMLEvents');
evt.initEvent('submit', true, true);
evt.element = function (){ return evt.target; };
form.dispatchEvent(evt);
document.getElementById('lat').value = document.getElementById('lon').value = '';
document.getElementById('display_coordinates').value = false;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment