-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin-imgur.html
More file actions
153 lines (145 loc) · 5.94 KB
/
Copy pathplugin-imgur.html
File metadata and controls
153 lines (145 loc) · 5.94 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
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
<template id="imgur-selector">
<div>
<a @click="pickImage()">
<i class="fa fa-camera" :class="[this.uploading ? 'fa-spin' : '']"></i>
<input
@change="uploadImage($event)"
id="ImgurUploader"
type="file"
accept=".jpg,.jpeg,.png,.gif,.apng,.tiff"
style="display: none; width: 0; height: 0;">
</a>
</div>
</template>
<template id="imgur-embeded">
<div>
<a :href="imgur" target="_blank">{{imgur}}</a>
<i
@click="toggleImage()"
:class="[show ? 'fa-angle-down' : 'fa-angle-right']"
class="fa fa-fw"
aria-hidden="true"
style="font-size: 1.3em; font-weight: bold; display: inline-block;"
></i>
<img
v-if="show"
:src="imgur"
style="display: block; max-height: 300px; cursor: pointer;"
>
</div>
</template>
<script>
kiwi.plugin('imgur', function(kiwi, log) {
// Generate a clientID here: https://api.imgur.com/oauth2/addclient
// Select: 'Anonymous usage without user authorization'
var IMGUR_API_KEY = '83ed9a388504288';
var IMGUR_MAX_SIZE = 10240;
// Default client settings
kiwi.state.setSetting('settings.pluginImgurAutoShow', true);
var imgurSelector = new kiwi.Vue({
template: '#imgur-selector',
data() {
return {
uploading: false,
};
},
methods: {
pickImage: function pickImage() {
if (this.uploading) {
return;
}
document.querySelector('#ImgurUploader').click();
},
uploadImage: function uploadImage(event) {
var file = event.target.files[0];
if (!file) {
return;
}
if (!this.isSupported(file.type)) {
this.sendMessage('Désolé, seules les images peuvent être téléchargées.', 'error');
return;
}
if (!file.size >= IMGUR_MAX_SIZE) {
this.sendMessage('Désolé, la taille du fichier est limitée à ${IMGUR_MAX_SIZE} octets, vous avez tenté de charger ${fichier.size} octets.', 'error');
return;
}
var self = this;
var fd = new FormData();
fd.append('image', file);
var xhttp = new XMLHttpRequest();
xhttp.open('POST', 'https://api.imgur.com/3/image');
xhttp.onload = function () {
try {
var resp = JSON.parse(xhttp.responseText);
if (resp.success && resp.data.link) {
var buffer = kiwi.state.getActiveBuffer();
buffer.say('Image partagée: ' + resp.data.link);
} else {
self.sendMessage('Une erreur a été signalée lors du téléchargement.' +
('error' in resp.data ? ' (' + resp.data.error.message + ')' : ''), 'error');
}
} catch (e) {
self.sendMessage('Une erreur a été interceptée lors du téléchargement (' + e.message + ')', 'error');
}
self.uploading = false;
};
xhttp.setRequestHeader('Authorization', 'Client-ID ' + IMGUR_API_KEY);
xhttp.send(fd);
this.uploading = true;
},
sendMessage: function sendMessage(message, type) {
var buffer = this.$state.getActiveBuffer();
this.$state.addMessage(buffer, {
time: Date.now(),
nick: '',
message: message,
type: type,
});
},
isSupported: function isSupported(fileType) {
if (fileType && fileType.match(/image\/.*/)) {
return true;
}
return false;
},
},
});
if (IMGUR_API_KEY === 'PUT YOUR IMGUR API KEY HERE') {
console.error('You forgot to add your imgur api key :(');
return;
}
kiwi.addUi('input', imgurSelector.$mount().$el);
// Embedding imgur urls into the message list
var imgurEmbeded = {
template: '#imgur-embeded',
data: function data() {
return {
imgur: '',
show: true,
};
},
mounted() {
this.show = this.$state.setting('pluginImgurAutoShow');
},
methods: {
toggleImage: function toggleImage() {
this.show = !this.show;
},
},
};
var imgurURL= RegExp('(^|\\s+)(https?://(i.imgur.com/[a-zA-Z0-9]+\\.\\w{3}))(\\s+|$)');
kiwi.state.$on('message.new', function postStyle(event) {
var message = event.message;
if (!message || event.type !== 'privmsg') {
return;
}
if (imgurURL.test(event.message)) {
var matches = event.message.match(imgurURL);
var imgurEmbededComponent = new kiwi.Vue(imgurEmbeded);
imgurEmbededComponent.imgur = 'https://' + matches[3];
imgurEmbededComponent.$mount();
event.bodyTemplate = imgurEmbededComponent;
}
});
});
</script>