-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
108 lines (91 loc) · 3.38 KB
/
content.js
File metadata and controls
108 lines (91 loc) · 3.38 KB
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
98
99
100
101
102
103
104
105
106
107
108
const isDebugging = true;
let observerDisabler = null;
let raidId = '';
// return disabler of the observer
const observeDOM = (function () {
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
return function (obj, callback) {
if (!obj || obj.nodeType !== 1) return;
let disabler = null;
if (MutationObserver) {
if (isDebugging) console.log('observeDOM: define a new observer.');
// define a new observer
const mutationObserver = new MutationObserver(callback)
// have the observer observe the object for changes in children
mutationObserver.observe(obj, {childList: true, subtree: true})
disabler = function() {
if (isDebugging) console.log('disconnect mutation observer.');
mutationObserver.disconnect();
};
}
// browser support event listener
else if (window.addEventListener) {
if (isDebugging) console.log('observeDOM: browser support event listener.');
obj.addEventListener('DOMNodeInserted', callback, false)
obj.addEventListener('DOMNodeRemoved', callback, false)
disabler = function() {
if (isDebugging) console.log('remove event listener.');
obj.remove('DOMNodeInserted', callback);
obj.remove('DOMNodeRemoved', callback);
};
} else {
if (isDebugging) console.log('observeDOM: failed to observe DOM.');
}
return disabler;
}
})()
const copyToClipboard = function(text) {
navigator.clipboard.writeText(text).then(function() {
if (isDebugging) console.log('Copying to clipboard was successful!', text);
}, function(err) {
if (isDebugging) console.log('Could not copy text: ', err);
});
}
const enableAutoCopy = function() {
if (isDebugging) console.log('enable auto-copy.');
const classNames = 'mdl-list gbfrf-tweets';
const elements = document.getElementsByClassName(classNames);
const element = elements[0];
// save the returned disabler
observerDisabler = observeDOM(element, function (m) {
// save the Raid-ID of the first element
const recordClassNames = 'gbfrf-tweet gbfrf-js-tweet mdl-list__item';
const records = element.getElementsByClassName(recordClassNames);
const firstRecord = records[0];
const keyRaidId = 'data-raidid';
const newRaidId = firstRecord.getAttribute(keyRaidId);
if (raidId !== newRaidId) {
raidId = newRaidId;
// copy to clipboard
copyToClipboard(raidId);
}
});
};
const disableAutoCopy = function() {
if (isDebugging) console.log('disable auto-copy.');
if (observerDisabler) {
observerDisabler();
}
};
// default enable auto-copy
enableAutoCopy();
// show the controller
const buttonTextAutoCopy = chrome.i18n.getMessage('buttonTextAutoCopy');
$('body').append(
`<div class="auto-copy">
<span class="title">${buttonTextAutoCopy}</span>
<input type="checkbox" checked />
<div>`
);
$('.auto-copy input[type="checkbox"]').change(function() {
if (this.checked) enableAutoCopy();
else disableAutoCopy();
});
// on blur
$(window).blur(function(e) {
copyToClipboard(raidId);
});
// on focus
$(window).focus(function(e) {
copyToClipboard(raidId);
});