/
main.js
67 lines (49 loc) · 1.33 KB
/
main.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
(function($) {
'use strict';
var IMG_PATTERN = /\[(t?img)-([^\]]+)\]/g;
var options;
function BuildUrl(filename) {
return options.RootUrl
? options.RootUrl + '/' + filename
: null
;
}
function ReplaceTags(text) {
var match;
var result = [];
var lastIndex = 0;
while (match = IMG_PATTERN.exec(text)) {
var keyword = match[1];
var filename = match[2];
if (!/\./.test(filename)) {
filename += '.' + (options.ImageExt === 'other' ? options.Other : options.ImageExt);
}
var url = BuildUrl(filename);
if (!url) {
result.push(text.substring(lastIndex, match.index + match[0].length));
}
else {
if (lastIndex < match.index) result.push(text.substring(lastIndex, match.index));
result.push('[' + keyword + ']' + url + '[/' + keyword + ']');
}
lastIndex = match.index + match[0].length;
}
if (result.length === 0) return text;
if (lastIndex < text.length) result.push(text.substr(lastIndex));
return result.join('');
}
function SubstituteTags() {
if (!options) return true;
var $post = $('.post-wrapper>textarea');
var text = $post.val();
text = ReplaceTags(text);
$post.val(text);
return true;
}
$(function() {
chrome.storage.local.get('options', function(root) {
options = root.options;
$('#content>form').submit(SubstituteTags);
});
});
})(jQuery);