-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
43 lines (32 loc) · 1.38 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
// content.js
chrome.runtime.sendMessage({type: 'onload'}, (response) => {
// do stuff with response (which will be the value of messageQueue
// sent from background.js).
});
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function (mutations, observer) {
// fired when a mutation occurs
console.log('somethings changing', mutations[0].target.style.cssText);
if (mutations[0].attributeName === 'style') {
const { target } = mutations[0];
const data = {
type: 'css_changed',
selector: target.className || target.localName,
styles: target.style.cssText,
// this is probably a unique identification for a DOM element as the offsets + id + class are rarely a repeated combination
elementID: target.offsetHeight + target.offsetLeft + target.offsetTop + target.offsetWidth + target.tagName + [...target.classList] + target.id
}
chrome.runtime.sendMessage(data, (response) => {
// do stuff with response (which will be the value of messageQueue
// sent from background.js).
});
};
// ...
});
// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document, {
subtree: true,
attributes: true
//...
});