Created
February 15, 2012 09:13
-
-
Save aschempp/1834638 to your computer and use it in GitHub Desktop.
Automatically create callto: and skype: links for phone numbers and contacts on Highrise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Highrise Links | |
// @author Andreas Schempp | |
// @description Create links for various resources on 37signals's Highrise (http://highrisehq.com) | |
// @include http://*highrisehq.com* | |
// @include https://*highrisehq.com* | |
// @version 2.0 | |
// ==/UserScript== | |
if (typeof unsafeWindow != 'undefined') { | |
window.$$ = window.$$ || unsafeWindow['window'].$$; | |
window.Element = window.Element || unsafeWindow['window'].Element; | |
} | |
var Highrise = Highrise || {}; | |
Highrise.PersonView = function() { | |
this.markupPhoneNumbers(); | |
}; | |
Highrise.PersonView.prototype.markupPhoneNumbers = function() | |
{ | |
var self = this; | |
$$('.phone_number .value').each( function(element) | |
{ | |
var matcher = element.innerHTML.match(/([^<]+)(?:(<span class="[^"]+">[^<]*<\/span>))/); | |
var phoneNumber = matcher[1].strip(); | |
var phoneType = matcher[2]; | |
if (self.isPhoneNumber(phoneNumber)) | |
{ | |
var linkedPhone = "<a href='callto:" + self.toCallable(phoneNumber) + "' class='callto'>" + phoneNumber + "</a>"; | |
element.innerHTML = linkedPhone + " " + phoneType; | |
} | |
}); | |
$$('.party .info h6').each( function(element) | |
{ | |
var matcher = element.innerHTML.match(/<div\b[^>]*>(.|\n)*?<\/div>[\n\t ]*(<a\b[^>]*>(.|\n)*?<\/a>[\n\t ]*)?([0-9 +]+)/i); | |
var phoneNumber = matcher[4]; | |
if (self.isPhoneNumber(phoneNumber)) | |
{ | |
var linkedPhone = "<a href='callto:" + self.toCallable(phoneNumber) + "' class='callto'>" + phoneNumber + "</a>"; | |
element.innerHTML = element.innerHTML.replace(phoneNumber, linkedPhone); | |
} | |
}); | |
}; | |
Highrise.PersonView.prototype.isPhoneNumber = function(phoneNumber) { | |
var digits = this.digitsOnly(phoneNumber); | |
return (digits && digits.length >= 10 && digits.length >= 7); | |
}; | |
Highrise.PersonView.prototype.digitsOnly = function(phoneNumber) { | |
return phoneNumber.replace(/[^\d+]/g,''); | |
}; | |
// see http://userscripts.org/scripts/review/10990 for an algorithm | |
// default international prefix should be user setting (e.g. +1 or +61) | |
Highrise.PersonView.prototype.toCallable = function(phoneNumber) { | |
var number = this.digitsOnly(phoneNumber); | |
number = number.replace(/^0/, ''); | |
if (/^\+/.test(number) || number.length == 10) { | |
return number; | |
} | |
return this.phonePrefix() + number; | |
}; | |
Highrise.PersonView.prototype.phonePrefix = function() { | |
// TODO - store in user preferences | |
return "+41"; | |
}; | |
// Activate each feature | |
new Highrise.PersonView(); | |
Highrise.Version = '2.0'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment