Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom url interception for beforeload #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion birdhouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,50 @@ function BirdHouse(params) {
}
}

webView.addEventListener('beforeload',function(){
function stringBeginsWith(url, prefix){
if (url.substr(0, prefix.length) == prefix) {
return true;
}
return false;
}

// If callback_url is specified when creating the BH instance, it can
// be intercepted before the webview loads it.
// This tells us the auth step is done.
// For example:
// var BH = new BirdHouse({ ...
// callback_url: "myfineapp://callback"
// });
// When this fires the beforeload, we're done.
webView.addEventListener('beforeload',function(e){
if (cfg.callback_url !="" && stringBeginsWith(e.url, cfg.callback_url)) {
Ti.API.debug("beforeload intercepted " + e.url);

// Twitter has redirected the page to our custom callback URL
doinOurThing = true; // kill the timeout b/c we are doin our thing

// success!
params = "";
var parts = (e.url).replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
params = params + m;

if (key=='oauth_verifier') {
cfg.request_verifier = value;
}
});

if (cfg.request_verifier!="") {
webView.hide();
webView.stopLoading(true);
win.remove(webView);
win.close();

get_access_token(callback);

return; // we are done here
}
}

loading = true;
});
webView.addEventListener('load',function(e){
Expand Down