-
Notifications
You must be signed in to change notification settings - Fork 5
/
iframe.js
76 lines (71 loc) · 2.38 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
73
74
75
76
/*
* This script is injected into all frames of a tab when the sidebar has
* been created. 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){
var port = chrome.extension.connect({
name: 'sidebar-iframe'
});
// Listen for requests from the background page.
port.onMessage.addListener(function(msg) {
if (msg.action === 'reload'){
window.location.reload();
}
else {
console.log('iframe received unrecognized message from background: ', msg);
}
});
close.addEventListener(
'click',
function(evt){
port.postMessage({
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');
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).
port.postMessage({
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.
port.postMessage({
action: 'open',
docURL: document.location.toString(),
linkURL: url
});
evt.preventDefault();
evt.stopPropagation();
return false;
}
else {
return true;
}
},
true
);
}