-
Notifications
You must be signed in to change notification settings - Fork 2
/
iframe.js
72 lines (69 loc) · 2.37 KB
/
iframe.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
/*
* This script is injected into all windows.
*
* It should only do things if it's in the sidebar frame, not
* in the main tab code.
*/
var close = document.getElementById('lovemedo-sidebar-close');
if (close) {
// Listen for requests from the background page.
var handleMessage = function(e) {
var msg = e.message;
if (e.name === 'background' && msg.action === 'reload') {
window.location.reload();
}
};
safari.self.addEventListener('message', handleMessage, false);
close.addEventListener(
'click',
function(evt) {
safari.self.tab.dispatchMessage('iframe', {
action: 'hide sidebar'
});
return false;
},
false
);
document.getElementsByTagName('body')[0].addEventListener(
'click',
function(evt) {
// Intercept click events on links and send a message to the
// background page so they can be displayed in the current tab.
var url = null;
if (evt.target.nodeName === 'A') {
url = evt.target.getAttribute('href');
}
else if (evt.target.nodeName === 'IMG') {
url = evt.target.parentNode.getAttribute('href');
console.log(evt);
if (url && url.slice(0, 17) === '/login/fluidinfo/') {
// The user is trying to log in. Ask the background
// page to start that process (we can't do it here as
// we're just a lowly iframe).
safari.self.tab.dispatchMessage('iframe', {
action: 'oauth login'
});
evt.preventDefault();
evt.stopPropagation();
return false;
}
}
if (url) {
// Ask the background page to open the link in the tab that
// created us.
safari.self.tab.dispatchMessage('iframe', {
action: 'open',
docURL: document.location.toString(),
linkURL: url
});
evt.preventDefault();
evt.stopPropagation();
return false;
}
else {
return true;
}
},
true
);
}