This repository has been archived by the owner on Aug 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatwork-replace-icon.user.js
73 lines (57 loc) · 2.08 KB
/
chatwork-replace-icon.user.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
// ==UserScript==
// @name ChatWork Replace Icon
// @description Replace user icon for ChatWork.
// @namespace https://github.com/chocoby/chatwork-replace-icon
// @include https://www.chatwork.com/*
// @version 0.0.3
// ==/UserScript==
// EDIT BEGIN
var userId = ""; // ex. "XXXXXX" (data-aid="123456")
var newIconUrl = ""; // ex. "http://example.com/path/to/image.jpg"
// EDIT END
var apiEndpoint = "http://chatwork-allow-origin-proxy.herokuapp.com/v1/image.json?url="
var imageDataURL;
var timer = 0;
function findIcon() {
if (timer) return;
timer = setTimeout(function() {
var xpath = document.evaluate(getXpathExpression(), document, null, 7, null);
var xpathLength = xpath.snapshotLength;
for (var i = 0; i < xpathLength; i++) {
fetchAndReplaceIcon(newIconUrl, xpath.snapshotItem(i));
}
timer = 0;
}, 5);
};
function fetchAndReplaceIcon(iconUrl, icon) {
if (!imageDataURL) {
imageDataURL = localStorage.getItem(iconUrl);
}
if (imageDataURL) {
icon.setAttribute("src", imageDataURL);
} else {
console.log("no cache. fetching icon...");
var xhr = new XMLHttpRequest();
var requestUrl = apiEndpoint + iconUrl;
xhr.open('GET', requestUrl, true);
xhr.addEventListener("load", function() {
if (xhr.readyState == 4) {
var res = JSON.parse(xhr.response);
if (xhr.status === 200) {
imageDataURL = res.data;
icon.setAttribute("src", imageDataURL);
localStorage.setItem(iconUrl, imageDataURL);
console.log("icon cached");
} else {
var statusMessage = res.status;
console.log("icon fetch error: " + statusMessage);
}
}
}, false);
xhr.send();
}
}
function getXpathExpression() {
return './/img[contains(concat(" ", @data-aid, " "), " ' + userId + ' ")]';
}
window.addEventListener("DOMNodeInserted", findIcon, false);