-
Notifications
You must be signed in to change notification settings - Fork 4
/
image_changer.js
213 lines (196 loc) · 8.84 KB
/
image_changer.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/* jshint esversion:6 */
var ImageChanger = function(settings, config) {
this.current_settings = settings;
this.current_config = config;
};
// this routine starts with an element and tries to work up
// the hierarchy to see if it is enclosed anywhere by an 'a',
// and then if that a's link matches the regex supplies, we
// return true.
ImageChanger.prototype.findParentLinkMatch = function(elem, re) {
var done = false;
var celem = elem;
var count = 0;
while (count < 15) {
if (celem && (celem.nodeName === 'A')) {
var ss = decodeURIComponent(celem.href);
if (celem.href) {
if (re.exec(decodeURIComponent(celem.href))) {
return true;
}
return false;
}
}
count += 1;
celem = celem.parentElement;
if (celem === null) {
return false;
}
}
return false;
};
ImageChanger.prototype.makeImageReplacementDiv = function(img, action) {
var border = 2;
var nd = document.createElement('div');
var newimg = document.createElement('img');
newimg.setAttribute('detrumpified', true);
newimg.alt = 'National disgrace replaced.';
newimg.title = 'National disgrace replaced.';
var newimgsrc = chrome.runtime.getURL('empty_image.png');
if (action.hasOwnProperty('image_replacement')) {
var idx = Math.floor(Math.random() * action.image_replacement.html.length);
newimg.alt = action.image_replacement.html[idx];
border = action.image_replacement.border;
if (action.image_replacement.hasOwnProperty('background')) {
idx = Math.floor(Math.random() * action.image_replacement.background.length);
newimgsrc = action.image_replacement.background[idx];
}
}
var dw = img.clientWidth - 2 * border;
var dh = img.clientHeight - 2 * border;
nd.style['text-align'] = 'center';
nd.style['vertical-align'] = 'middle';
nd.style.display = 'table-cell';
nd.style.border = border.toString() + 'px solid';
nd.style.width = dw.toString() + 'px';
nd.style['max-width'] = dw.toString() + 'px';
nd.style.height = dh.toString() + 'px';
nd.style['max-height'] = dh.toString() + 'px';
removeChildrenReplaceWith(nd, [newimg]);
newimg.src = newimgsrc;
// very primitive way to deal with the aspect ratio. Assume that
// the longer edge is more important to match, don't do anything
// else. Will have to revisit this, but not today.
if (dw > dh) {
newimg.width = '100%';
} else {
newimg.height = '100%';
}
return nd;
};
var swapOldNewSrc = function(elem) {
var old_src = elem.getAttribute('old_src');
if (old_src) {
var new_old_src = elem.src;
elem.setAttribute('old_src',new_old_src);
elem.src = old_src;
}
};
var unReplaceEventHandler = function(ev) {
ev.preventDefault();
swapOldNewSrc(ev.target);
ev.target.removeEventListener('click',unReplaceEventHandler);
};
ImageChanger.prototype.run = function(imgs = null) {
log('switch_imgs()');
// we do not need to do anything if we get a zero length list
if (imgs && !imgs.length) return;
var tthis = this;
if (true) {
var action_count = 0;
var imgreplsrc = useIfElse(this.current_settings, 'imgreplsrc', '__off__');
var src_not_url = imgreplsrc.match(/^__(\w+)__$/);
var mode = 'off';
if (src_not_url) {
mode = src_not_url[1];
}
if (imgreplsrc.match(/^https?:/)) {
mode = 'replcfg';
}
var picdb = null;
if (mode == 'replcfg') {
log('mode is replcfg');
if (this.current_settings.hasOwnProperty('imgrepldata')) {
picdb = new PicDB();
// log(this.current_settings.imgrepldata);
picdb.loadDirect(this.current_settings.imgrepldata || []);
picdb.processData(this.current_settings.imgreplsrc + '/');
} else {
log('imgrepl won\'t work');
mode = 'off';
}
}
if (mode !== 'off') {
var actions_to_run = getRunnableActions(tthis.current_config.actions, this.current_settings);
for (var n = 0; n < actions_to_run.length; n++) {
action_name = actions_to_run[n];
var action = tthis.current_config.actions[action_name];
var alt_re = new RegExp(action.find_regex[0],
action.find_regex[1]);
var src_re;
if (action.hasOwnProperty('img_find_regex')) {
src_re = new RegExp(action.img_find_regex[0],
action.img_find_regex[1]);
} else {
src_re = new RegExp(action.find_regex[0], 'i');
}
if (!imgs) imgs = document.getElementsByTagName('img');
for (var i = 0; i < imgs.length; i++) {
var img = imgs[i];
var one_of_ours = img.getAttribute('detrumpified');
if (one_of_ours) {
log('already detrumpified: ' + img.src);
break;
}
// Super-sophisticated image detection algorithm here:
// -- does the alt text look trumpian?
var alt_match = alt_re.exec(img.alt);
// does the image source itself look trumpian?
var src_match = src_re.exec(decodeURIComponent(img.src));
// is there anything trumpian in the style tag?
var sty_match = src_re.exec(decodeURIComponent(img.style));
// is there an enclosing "A" parent whose href looks trumpian?
var parent_link_match = false;
try {
parent_link_match = tthis.findParentLinkMatch(img, src_re);
} catch (w) {}
if (alt_match || src_match || sty_match || parent_link_match) {
log('alt: ' + alt_match + ' src: ' + src_match + ' sty: ' + sty_match +
' prnt: ' + parent_link_match);
var replsrc;
var ni = null;
log('looking to replace: ' + img.src);
var iw = img.clientWidth;
var ih = img.clientHeight;
if (mode == 'div') {
var nd = tthis.makeImageReplacementDiv(img, action);
img.parentNode.replaceChild(nd, img);
} else if ((mode == 'kittens') || (mode == 'blanks')) {
replsrc = (mode == 'kittens') ?
'https://placekitten.com/' :
'https://placehold.it/';
replsrc += iw.toString() + '/' + ih.toString();
if (img.src !== replsrc) {
log('[kitten/blank]: replacing ' + img.src + ' with ' + replsrc);
ni = document.createElement('img');
ni.src = replsrc;
ni.setAttribute('old_src',img.src);
ni.addEventListener('click', unReplaceEventHandler);
ni.title = 'Click to re-trumpify image';
img.parentNode.replaceChild(ni, img);
}
} else if (mode == 'replcfg') {
replsrc = picdb.selectImage(iw, ih);
if (replsrc) {
if (!img.getAttribute('replcfg_detrumpified')) {
log('[replcfg]: replacing ' + img.src + ' with ' + replsrc);
ni = document.createElement('img');
ni.title = 'Click to re-trumpify image';
ni.style.width = Math.floor(iw + 0.5).toString() + 'px';
ni.style.height = Math.floor(ih + 0.5).toString() + 'px';
ni.setAttribute('replcfg_detrumpified', true);
ni.setAttribute('old_src',img.src);
ni.src = replsrc;
img.parentNode.replaceChild(ni, img);
ni.addEventListener('click', unReplaceEventHandler);
}
} else {
log('No appropriate replacement image for ' + img.src + ' (' + iw + ',' + ih + ')');
}
}
}
}
}
}
}
};