Skip to content

Commit

Permalink
Seamlessly supporting identi.ca microblogging service. Just switch th…
Browse files Browse the repository at this point in the history
…e microblogging service in the options page. Closes #90 and closes #28
  • Loading branch information
cezarsa committed Mar 5, 2010
1 parent c2371fc commit a73a083
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 26 deletions.
16 changes: 15 additions & 1 deletion background.html
Expand Up @@ -31,6 +31,8 @@
compose_position: 'top',
hover_timeout: 1000,

microblogging_service: 'twitter',

base_url: 'http://api.twitter.com/1/',
base_oauth_url: 'https://twitter.com/oauth/',

Expand Down Expand Up @@ -249,7 +251,9 @@
checkMaxTweet('favorites');
}

usedTweetsMap[maxBeforeTweet.id] = true;
if(maxBeforeTweet) {
usedTweetsMap[maxBeforeTweet.id] = true;
}
return maxBeforeTweet;
}

Expand Down Expand Up @@ -737,6 +741,15 @@
this.selectedListsData = Persistence.selectedLists();
this.timelineOrderData = Persistence.timelineOrder();
this.oauthTokenData = Persistence.oauthTokenData();
this.oauthTokenService = Persistence.oauthTokenService();

var currentService = OptionsBackend.get('microblogging_service');
if(!this.oauthTokenService.val()) {
this.oauthTokenService.save(currentService);
} else if(currentService != this.oauthTokenService.val()) {
this.oauthTokenService.save(currentService);
this.oauthTokenData.remove();
}

var timelineNames = ["home", "mentions", "dms", "favorites", "lists"];
for(var i = 0; i < timelineNames.length; ++i) {
Expand Down Expand Up @@ -806,6 +819,7 @@
function(remainingHits, nextHitsReset, hourlyLimit) {
_this.onHitsUpdated.call(_this, remainingHits, nextHitsReset, hourlyLimit);
},
OptionsBackend.get('microblogging_service'),
OptionsBackend.get('base_url'),
OptionsBackend.get('base_oauth_url'),
OptionsBackend.get('base_signing_url'),
Expand Down
7 changes: 7 additions & 0 deletions lib/oauth_identica_authorizer.js
@@ -0,0 +1,7 @@
function authorize() {
var requestToken = $('p').text().match("token (.*) has")[1];
chrome.extension.sendRequest({identica_request_token: requestToken}, function(response) {

});
}
authorize();
2 changes: 1 addition & 1 deletion lib/persistence.js
Expand Up @@ -7,7 +7,7 @@ Persistence = {
/* Creating helper methods for each persistence entry (load() shoudn't be used directly) */
init: function() {
var existingKeys = ['options', 'selected_lists', 'timeline_order', 'oauth_token_data',
'version', 'popup_size'];
'oauth_token_service', 'version', 'popup_size'];

var _this = this;
for(var i = 0, len = existingKeys.length; i < len; ++i) {
Expand Down
70 changes: 54 additions & 16 deletions lib/twitter_lib.js
@@ -1,13 +1,20 @@
function TwitterLib(requestTimeout, onAuthenticated, onHitsUpdated, baseUrl, baseOauthUrl, baseSigningUrl, baseOauthSigningUrl, oauthTokenData) {
function TwitterLib(requestTimeout, onAuthenticated, onHitsUpdated, microbloggingService, baseUrl, baseOauthUrl, baseSigningUrl, baseOauthSigningUrl, oauthTokenData) {
this.remainingHitsCount = 150;
this.nextHitsReset = 0;
this.onAuthenticated = onAuthenticated;
this.onHitsUpdated = onHitsUpdated;
this.hourlyLimit = 150;
this.microbloggingService = microbloggingService;
var _this = this;
this.oauthLib = new TwitterOAuth(oauthTokenData, function() {
_this.updateHourlyHitsLimit.call(_this)
_this.onAuthenticated();
this.oauthLib = new TwitterOAuth(microbloggingService, oauthTokenData, function() {
_this.updateHourlyHitsLimit.call(_this);
if(!_this.username()) {
_this.verifyCredentials(function() {
_this.onAuthenticated();
});
} else {
_this.onAuthenticated();
}
});
this.requestTimeout = requestTimeout;

Expand Down Expand Up @@ -54,17 +61,24 @@ TwitterLib.prototype = {
callback(true, data, status, context);
},
error: function (request, status, error) {
var fmtError = '';
if(status == 'timeout') {
//_this.verifyCredentials();
//TODO: Currently 401 responses aren't returned, they just timeout, there's nothing I
//can do right now to fix this.
fmtError = "(timeout)";
} else {
var fmtError = '';
try {
if(!request.responseText) {
throw 'no response';
}
var rspObj = JSON.parse(request.responseText);
fmtError = '"' + rspObj.error + '"(' + request.statusText + ')';
if(_this.microbloggingService == 'identica') {
var txt = request.responseText;
fmtError = '"' + txt.replace(/<.*>(.*)<.*>/, '$1') + '"';
} else {
var rspObj = JSON.parse(request.responseText);
fmtError = '"' + rspObj.error + '"(' + request.statusText + ')';
}
} catch(e) {
fmtError = '"' + error + '"(' + status + ')';
}
Expand Down Expand Up @@ -96,12 +110,16 @@ TwitterLib.prototype = {
}
},

verifyCredentials: function() {
//TODO: Currently 401 responses aren't returned, they just timeout, there's nothing I
//can do right now to fix this.

/*this.ajaxRequest("account/verify_credentials", function(success, data) {
});*/
verifyCredentials: function(callback) {
var _this = this;
this.ajaxRequest("account/verify_credentials", function(success, data) {
if(success) {
_this.oauthLib.screen_name = data.screen_name;
}
if(callback) {
callback(success, data);
}
});
},

remainingHitsInfo: function() {
Expand Down Expand Up @@ -158,6 +176,16 @@ var globalOAuthInstance;
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if(!globalOAuthInstance)
return;
if(request.identica_request_token) {
if(!globalOAuthInstance.authenticated && globalOAuthInstance.tokenRequested) {
if(globalOAuthInstance.oauth_token == request.identica_request_token) {
globalOAuthInstance.authenticating = true;
globalOAuthInstance.getAccessToken.call(globalOAuthInstance, '', function() {
chrome.tabs.remove(sender.tab.id);
});
}
}
}
if(request.check_pin_needed) {
if(!globalOAuthInstance.authenticated && globalOAuthInstance.tokenRequested) {
sendResponse({});
Expand All @@ -171,7 +199,7 @@ chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
}
});

function TwitterOAuth(oauthTokenData, onAuthenticated) {
function TwitterOAuth(microbloggingService, oauthTokenData, onAuthenticated) {
this.user_id = null;
this.screen_name = null;
this.authenticated = false;
Expand All @@ -181,6 +209,16 @@ function TwitterOAuth(oauthTokenData, onAuthenticated) {
this.tokenRequested = false;
this.timeAdjusted = false;
this.oauthTokenData = oauthTokenData;
this.microbloggingService = microbloggingService;

if(microbloggingService == 'twitter') {
this.consumerSecret = 'MsuvABdvwSn2ezvdQzN4uiRR44JK8jESTIJ1hrhe0U';
this.consumerKey = 'KkrTiBu0hEMJ9dqS3YCxw';
} else if(microbloggingService == 'identica') {
this.consumerSecret = '5160eba9484e97fa164acd7fd5aa9b83';
this.consumerKey = '4f7780c1329c67a3d69c84c11a4edf9d';
}

globalOAuthInstance = this;

var _this = this;
Expand All @@ -201,7 +239,7 @@ TwitterOAuth.prototype = {
},
prepareSignedParams: function(url, params, httpMethod) {
var accessor = {
consumerSecret: 'MsuvABdvwSn2ezvdQzN4uiRR44JK8jESTIJ1hrhe0U',
consumerSecret: this.consumerSecret,
tokenSecret: this.oauth_token_secret
};
if(!httpMethod)
Expand All @@ -210,7 +248,7 @@ TwitterOAuth.prototype = {
action: url,
method: httpMethod,
parameters: [
['oauth_consumer_key', 'KkrTiBu0hEMJ9dqS3YCxw'],
['oauth_consumer_key', this.consumerKey],
['oauth_signature_method', 'HMAC-SHA1']
]
};
Expand Down
4 changes: 4 additions & 0 deletions manifest.json
Expand Up @@ -22,6 +22,10 @@
{
"matches": ["http://twitter.com/oauth/authorize", "https://twitter.com/oauth/authorize"],
"js": ["lib/3rdparty/jquery.js", "lib/oauth_authorizer.js"]
},
{
"matches": ["http://identi.ca/api/oauth/authorize", "https://identi.ca/api/oauth/authorize"],
"js": ["lib/3rdparty/jquery.js", "lib/oauth_identica_authorizer.js"]
}
],
"options_page": "options.html",
Expand Down
44 changes: 42 additions & 2 deletions options.html
Expand Up @@ -35,6 +35,10 @@
Converters[converter].load($this, _this.optionsMap[name]);
} else if($this.is('[type="checkbox"]')) {
$this.attr('checked', _this.optionsMap[name]);
} else if($this.is('[type="radio"]')) {
if(_this.optionsMap[name] == $this.val()) {
$this.attr('checked', true);
}
} else if($this.is('.color_selector')) {
$this.attr('strColor', _this.optionsMap[name]);
$this.ColorPickerSetColor(_this.optionsMap[name]);
Expand Down Expand Up @@ -96,6 +100,11 @@
newValue = Converters[converter].save($this);
} else if($this.is('[type="checkbox"]')) {
newValue = $this.attr('checked');
} else if($this.is('[type="radio"]')) {
if(!$this.attr('checked')) {
return true;
}
newValue = $this.val();
} else if($this.is('.color_selector')) {
newValue = $this.attr('strColor');
} else {
Expand Down Expand Up @@ -342,6 +351,33 @@
.click(updatePredictedHitsCount)
.change(updatePredictedHitsCount);
}

$("input[name='microblogging_service']").mousedown(function(e) {
var el = $(e.target);
if(!el.is(':checked')) {
var proceed = confirm("This will reset any custom API URL configuration. Proceed?");
if(proceed) {
el.click();
var sameSigningEl = $("input[name='same_signing_urls']");
var baseUrlEl = $("input[name='base_url']");
var baseOauthUrlEl = $("input[name='base_oauth_url']");

if(el.val() == 'twitter') {
baseUrlEl.val('http://api.twitter.com/1/');
baseOauthUrlEl.val('https://twitter.com/oauth/');
} else if(el.val() == 'identica') {
baseUrlEl.val('http://identi.ca/api/');
baseOauthUrlEl.val('https://identi.ca/api/oauth/');
}
baseUrlEl.blur();
baseOauthUrlEl.blur();
if(!sameSigningEl.is(':checked')) {
sameSigningEl.attr('checked', true).click().attr('checked', true);
}
}
}
return true;
});
});

function updatePredictedHitsCount() {
Expand All @@ -360,7 +396,6 @@
continue;
}


var inputRefreshEl = $('input[name="' + timelines[i] + '_refresh_interval"]');
var intVal = parseInt(inputRefreshEl.val());
var timelineHits = (60 * 60) / intVal;
Expand All @@ -378,7 +413,6 @@
}
return totalHits;
}

</script>

</head>
Expand Down Expand Up @@ -597,6 +631,12 @@ <h1>Chromed Bird Options</h1>
<input type="text" name="max_cached_tweets" validator="required,number,positive" must_restart><br>
</fieldset>

<fieldset>
<legend>Microblogging Service</legend>
<input type="radio" name="microblogging_service" value="twitter" must_restart> Twitter
<input type="radio" name="microblogging_service" value="identica" must_restart> identi.ca
</fieldset>

<fieldset>
<legend>Advanced</legend>
<label for="request_timeout">Request Timeout (ms):</label>
Expand Down
24 changes: 18 additions & 6 deletions popup.html
Expand Up @@ -13,12 +13,24 @@
var tweetManager = chrome.extension.getBackgroundPage().TweetManager.instance;
var twitterBackend = tweetManager.twitterBackend;
var OptionsBackend = chrome.extension.getBackgroundPage().OptionsBackend;
var TwitterLib = {
URLS: {
BASE: 'http://twitter.com/',
SEARCH: 'http://twitter.com/search?q='
}
};

var microbloggingService = OptionsBackend.get('microblogging_service');
var TwitterLib;
if(microbloggingService == 'twitter') {
TwitterLib = {
URLS: {
BASE: 'http://twitter.com/',
SEARCH: 'http://twitter.com/search?q='
}
};
} else if(microbloggingService == 'identica') {
TwitterLib = {
URLS: {
BASE: 'http://identi.ca/',
SEARCH: 'http://identi.ca/search/notice?q='
}
};
}

if(!twitterBackend.authenticated() && !twitterBackend.tokenRequested()) {
twitterBackend.startAuthentication();
Expand Down

0 comments on commit a73a083

Please sign in to comment.