-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.js
85 lines (75 loc) · 3.82 KB
/
script.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
(function() {
// this is adapted from http://stackoverflow.com/a/3550261
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
// call the callback with a noConflict jQuery object
script.textContent = "(" + callback.toString() + ")(jQuery.noConflict(true));";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
// main function with a scoped jQuery object
function main($) {
function scanForFrames() {
// chat frame ids start with "gtn_"
var frames = document.querySelectorAll('iframe[id^=gtn_]');
for (var i = 0; i < frames.length; i++) {
var frame = frames.item(i);
// If we're not already observing on this frame
if (!frame.hasOwnProperty("_fluidObserver")) {
// Name is in class="Ob2Lud RE EIhiV OxDpJ"
var nameNode = frame.contentWindow.document.querySelector(".Ob2Lud.RE.EIhiV.OxDpJ");
var name = nameNode.textContent.split(" ")[0];
// add the observer to "_fluidObserver" on the frame
frame._fluidObserver = new MutationObserver(function(records) {
for (var i in records) {
var record = records[i];
var $node = $(record.target);
// "tL8wMe xAWnQc" is a message div
// a parent with class "Sn" means it's their message (not ours)
if ($node.hasClass("tL8wMe") && $node.hasClass("xAWnQc") && $node.parents(".Sn").length > 0) {
var notification = new Notification(name, {
body: $node.text()
});
}
// "ZLer6" is an image div
// a parent with class "fHBzMd" and *without* "DKLL9c" means it's their image (not ours)
else if ($node.hasClass("ZLer6") && !$node.parents(".fHBzMd").hasClass("DKLL9c")) {
var notification = new Notification(name, {
body: "sent an image"
});
}
}
});
frame._fluidObserver.observe(frame.contentWindow.document.querySelector('.hN.so.Ij'), {
childList: true,
subtree: true
});
}
}
}
// Watch for new elements in the body
var parentObserver = new MutationObserver(function(records) {
for (var i in records) {
var record = records[i];
for (var j = 0; j < record.addedNodes.length; j++) {
var node = record.addedNodes.item(j);
// There seems to always be a spare "shiv_preld" sitting around, which new chats are placed into
// If we've added another shiv "preload" element, probably we've initialized a new chat frame
// Switch to indexOf since startsWith is missing in some instances of Fluid.
if (node.id.indexOf("shiv_preld_") === 0) {
scanForFrames();
}
}
}
});
parentObserver.observe(document.body, {
childList: true
});
}
// load jQuery and execute the main function
addJQuery(main);
})();