-
Notifications
You must be signed in to change notification settings - Fork 2
/
content.js
86 lines (78 loc) · 2.94 KB
/
content.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
var createOverListener = function(node) {
// Return a function that will send the innerText of a node to our
// background page. We'll use the functions returned as mouseover
// functions on links in (and added to) the document.
return function() {
// Send the link text, trimmed of leading/trailing whitespace and
// also the URL of the page.
safari.self.tab.dispatchMessage('content', {
mouseover: true,
docURL: document.location.toString().toLowerCase(),
linkURL: node.getAttribute('href'),
text: node.innerText.replace(/^\s+|\s+$/g, '')
});
return true;
};
};
var createOutListener = function(node) {
// Return a function that will send a mouseout message to the
// background page. We'll use the functions returned as mouseout
// functions on links in (and added to) the document.
return function() {
safari.self.tab.dispatchMessage('content', {mouseout: true});
return true;
};
};
var addListeners = function(nodes) {
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
node.addEventListener('mouseover', createOverListener(node));
node.addEventListener('mouseout', createOutListener(node));
}
};
// Add a mouse event listener for all <a> tags in the document once it has
// been loaded.
addListeners(document.getElementsByTagName('a'));
// Arrange to add a mouse event listener to all <a> tags that get added to
// the document.
var body = document.getElementsByTagName('body')[0];
body.addEventListener ('DOMNodeInserted', function(event) {
if (event.target.getElementsByTagName){
addListeners(event.target.getElementsByTagName('a'));
}
});
var checkSelection = function(event) {
var selection = window.getSelection().toString();
if (selection) {
safari.self.tab.dispatchMessage('content', {
selection: selection.replace(/^\s+|\s+$/g, '')
});
} else {
safari.self.tab.dispatchMessage('content', {selectionCleared: true});
}
};
document.addEventListener('mouseup', checkSelection, true);
document.addEventListener('keyup', checkSelection, true);
// -----------------------------------------------------------------------
// Context Menu
// -----------------------------------------------------------------------
var handleContextMenu = function(e) {
checkSelection();
var userInfo = {};
switch (e.target.nodeName) {
case 'IMG':
userInfo.about = e.target.src;
userInfo.type = 'IMG';
break;
default:
var text = document.getSelection().toString();
if (text === '') {
userInfo.about = document.URL;
userInfo.type = 'PAGE';
}
}
// always add url so we can send the referrer fragment
userInfo.url = document.URL;
safari.self.tab.setContextMenuEventUserInfo(e, userInfo);
};
document.addEventListener('contextmenu', handleContextMenu, false);