-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
99 lines (90 loc) · 3.83 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Capture image from tab */
function captureSelection(start, end, imageUrl) {
}
/* browser action */
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {file: "capture.js"});
chrome.tabs.insertCSS(tab.id, {file: "style.css"});
if (HTMLCanvasElement && !HTMLCanvasElement.prototype.toBlob) {
chrome.tabs.executeScript(null, {file: "canvas-toBlob.js"});
}
});
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
//var startPoint = request.start;
//var endPoint = request.end;
//var x = startPoint.x;
//var y = startPoint.y;
//var width = endPoint.x - startPoint.x;
//var height = endPoint.y - startPoint.y;
//capture whole window of selected tab
console.log(sendResponse);
if (request.action === 'capture') {
chrome.tabs.captureVisibleTab(null, function(imageUrl) {
sendResponse({image: imageUrl});
});
}
else if (request.action === 'activate') {
//loop through open window to find access token redirected url
var redirectedUrl = null;
console.log('here here is run');
/*
chrome.windows.onCreated.addListener(function(popup) {
//record the window id.
var popupId = popup.id;
console.log(popupId);
console.log(popup);
var popupWindow = chrome.windows.get(popupId, {populate: true}, function(windowWithTabs) {
var currentTab =
});
while (true) {
for (var i = 0; i < popup.tabs.length; i++) {
console.log(popup.tabs[i]);
if (popup.tabs[i].url.match('www.facebook.com/connect/login_success.html')) {
redirectedUrl = popup.tabs[i].url;
break;
}
}
//if successfully retrieve the url
if (redirectedUrl) {
sendResponse({accessToken: redirectedUrl});
break;
}
}
});
*/
getSuccessLoginUrl(sendResponse);
}
});
function getSuccessLoginUrl(sendBackData) {
chrome.windows.getAll({populate: true}, function (windows) {
var successUrl = null;
var loginWindowId = null;
for (var i = 0; i < windows.length; i++) {
var w = windows[i];
if (w.type === 'popup') {
//if it is facebook popup
var url = w.tabs[0].url;
if (url.indexOf('https://www.facebook.com/connect/login_success.html') != -1) {
successUrl = url;
loginWindowId = w.id;
break;
}
}
}
console.log(loginWindowId);
if (successUrl != null) {
console.log(successUrl);
var accessToken = successUrl.match('access_token=(.*?)&')[1];
var expireIn = successUrl.match('expires_in=([0-9]*)')[1];
//chrome.windows.remove(w.id, function() {console.log('closed the logged in popup');});
//sendResponse({'accessToken': accessToken, 'expireIn': expireIn});
var authorizationData = {'accessToken': accessToken, 'expireIn': expireIn};
sendBackData(authorizationData);
//return authorizationData;
chrome.windows.remove(loginWindowId, function() {console.log('closed the logged in popup');});
}
else {
getSuccessLoginUrl(sendBackData);
}
});
}