-
Notifications
You must be signed in to change notification settings - Fork 0
/
eraseDealerMutation.js
82 lines (67 loc) · 3.36 KB
/
eraseDealerMutation.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
function eraseDealerInitial(){
const postTable = frameWindow.document.querySelectorAll('#main-area > div.article-board.m-tcol-c')[1];
const dealerLink = 'https://cafe.pstatic.net/levelicon/1/1_150.gif';
if(postTable !== undefined){
let postElements = postTable.querySelectorAll('div.article-board.m-tcol-c > table > tbody > tr');
chrome.storage.local.get(['eraseList', 'eraseVIP'], function(data){
const isDeleteVIP = data['eraseVIP'];
for(let idx=postElements.length-1; idx>=0; idx--){
const src = postElements[idx].querySelector('td.td_name > div > table > tbody > tr > td > span > img').getAttribute('src');
const nickName = postElements[idx].querySelector('td.td_name > div > table > tbody > tr > td > a').textContent;
if(isDeleteVIP && src === dealerLink){
postElements[idx].setAttribute('style', 'display: none;');
} else if(data['eraseList'] && data['eraseList'].includes(nickName)){
postElements[idx].setAttribute('style', 'display: none;');
}
}
});
}
}
function checkFrameReady(e) {
if (!e.type) {
// DOMException occur until iframe is identified as same-origin as main frame
try {
start_observer(new frameWindow.MutationObserver(eraseDealer));
frameWindow.addEventListener('unload', checkFrameReady);
return; // to prevent unlimited recursive function call
} catch (e) {}
}
// If DOMException occur, checkFrameReady is called again.
requestAnimationFrame(checkFrameReady);
}
function eraseDealer(mutations, observer) {
const SEL = '.td_name img[src="https://cafe.pstatic.net/levelicon/1/1_150.gif"]';
const NAME = '.td_name a'
let stopped;
// mutations have changes as a list of MutationRecord https://javascript.info/mutation-observer
if(typeof mutations === 'undefined') return
chrome.storage.local.get(['eraseList', 'eraseVIP'], function(data) {
const isDeleteVIP = data['eraseVIP'];
for (const { addedNodes } of mutations) {
for (const n of addedNodes) {
// pass when encounter text
// ex) If there are <div>hi</div>, 'hi' text can be addedNoes
if (!n.tagName){
continue;
}
// check addedNode is correct to what want to erase
// if n is element what want to erase or n has grand child element what want to erase if n has child element
const elems = isDeleteVIP && n.matches(SEL) && [n] ||
n.matches(NAME) && data['eraseList'] && data['eraseList'].includes(n.text) && [n];
if (!elems) {
continue
}
if (!stopped) { stopped = true; observer.disconnect(); }
elems.forEach(el => el.closest('.td_name').closest('tr').setAttribute('style', 'display: none;'));
}
}
if (stopped) start_observer(observer);
});
}
function start_observer(observer) {
observer.observe(frameWindow.document.body || frameWindow.document.documentElement,
{childList: true, subtree: true});
}
var frameWindow = document.querySelector('#cafe_main').contentWindow;
eraseDealerInitial();
frameWindow.addEventListener('unload', checkFrameReady);