|
1 |
| -$(function () { |
2 |
| - const keys = ['ak', 'sk', 'bucket', 'domain']; |
3 |
| - let isConfig = true; |
| 1 | +$(function() { |
| 2 | + const keys = ['ak', 'sk', 'bucket', 'domain'] |
| 3 | + let isConfig = true |
4 | 4 |
|
5 |
| - keys.forEach(key => { |
6 |
| - if (!localStorage[key]) { |
7 |
| - isConfig = false; |
8 |
| - } |
9 |
| - }) |
10 |
| - |
11 |
| - if (!isConfig) { |
12 |
| - chrome.tabs.create({url: 'options.html'}); |
| 5 | + keys.forEach(key => { |
| 6 | + if (!localStorage[key]) { |
| 7 | + isConfig = false |
13 | 8 | }
|
14 |
| - const uptoken = genUpToken(localStorage['ak'], localStorage['sk'], localStorage['bucket']); |
15 |
| - const domain = localStorage['domain'].indexOf('http') == -1 ? 'http://' + localStorage['domain'] : localStorage['domain']; |
16 |
| - var uploader = Qiniu.uploader({ |
17 |
| - disable_statistics_report: false, |
18 |
| - runtimes: 'html5,flash,html4', |
19 |
| - browse_button: 'pickfiles', |
20 |
| - container: 'container', |
21 |
| - drop_element: 'container', |
22 |
| - max_file_size: '1000mb', |
23 |
| - flash_swf_url: 'bower_components/plupload/js/Moxie.swf', |
24 |
| - dragdrop: true, |
25 |
| - chunk_size: '4mb', |
26 |
| - multi_selection: !(moxie.core.utils.Env.OS.toLowerCase() === "ios"), |
27 |
| - uptoken: uptoken, |
28 |
| - domain: 'https://upload.qbox.me', |
29 |
| - get_new_uptoken: false, |
30 |
| - auto_start: true, |
31 |
| - log_level: 5, |
32 |
| - init: { |
33 |
| - 'BeforeChunkUpload': function (up, file) { |
34 |
| - console.log("before chunk upload:", file.name); |
35 |
| - }, |
36 |
| - 'FilesAdded': function (up, files) { |
37 |
| - $('.words').html('上传中...'); |
38 |
| - }, |
39 |
| - 'BeforeUpload': function (up, file) { |
40 |
| - console.log("this is a beforeupload function from init"); |
41 |
| - var chunk_size = plupload.parseSize(this.getOption( |
42 |
| - 'chunk_size')); |
43 |
| - }, |
44 |
| - 'UploadProgress': function (up, file) { |
45 |
| - }, |
46 |
| - 'UploadComplete': function () { |
47 |
| - $('.words').html('上传完成'); |
48 |
| - $('.status').css('display', 'flex'); |
49 |
| - setTimeout(() => { |
50 |
| - $('.status').css('display', 'none'); |
51 |
| - }, 3000); |
52 |
| - $('#link-area').css('visibility', 'visible'); |
| 9 | + }) |
| 10 | + |
| 11 | + if (!isConfig) { |
| 12 | + chrome.tabs.create({ url: 'options.html' }) |
| 13 | + return |
| 14 | + } |
| 15 | + const uptoken = genUpToken( |
| 16 | + localStorage['ak'], |
| 17 | + localStorage['sk'], |
| 18 | + localStorage['bucket'] |
| 19 | + ) |
53 | 20 |
|
54 |
| - }, |
55 |
| - 'FileUploaded': function (up, file, info) { |
56 |
| - console.log("response:", info.response); |
57 |
| - res = info.response; |
58 |
| - res = JSON.parse(res); |
59 |
| - const link = domain + '/' + res.key; |
60 |
| - const markdownLink = ''; |
61 |
| - const aLink = '<a href="' + link + '"><img src="' + link + '" alt="' + res.key + '"></a>'; |
62 |
| - $('#link').val(link); |
63 |
| - $('#markdown').val(markdownLink); |
64 |
| - $('#a-link').val(aLink); |
65 |
| - }, |
66 |
| - 'Error': function (up, err, errTip) { |
67 |
| - if (errTip.indexOf('401') !== -1) { |
68 |
| - errTip = "鉴权失败,请确定AK/SK配置正确" |
69 |
| - } |
70 |
| - $('.words').html(`上传失败: ${errTip}`); |
71 |
| - $('.status').css('display', 'flex'); |
72 |
| - $('.status img').css('display', 'none'); |
73 |
| - setTimeout(() => { |
74 |
| - $('.status').css('display', 'none'); |
75 |
| - $('.status img').css('display', 'unset'); |
76 |
| - }, 5000); |
77 |
| - $('#link-area').css('visibility', 'visible'); |
78 |
| - } |
79 |
| - } |
80 |
| - }); |
| 21 | + const domain = |
| 22 | + localStorage['domain'].indexOf('http') == -1 |
| 23 | + ? 'http://' + localStorage['domain'] |
| 24 | + : localStorage['domain'] |
81 | 25 |
|
82 |
| - $('#container').on( |
83 |
| - 'dragenter', |
84 |
| - function (e) { |
85 |
| - e.preventDefault(); |
86 |
| - $('#container').addClass('draging'); |
87 |
| - e.stopPropagation(); |
88 |
| - } |
89 |
| - ).on('drop', function (e) { |
90 |
| - e.preventDefault(); |
91 |
| - $('#container').removeClass('draging'); |
92 |
| - e.stopPropagation(); |
93 |
| - }).on('dragleave', function (e) { |
94 |
| - e.preventDefault(); |
95 |
| - $('#container').removeClass('draging'); |
96 |
| - e.stopPropagation(); |
97 |
| - }).on('dragover', function (e) { |
98 |
| - e.preventDefault(); |
99 |
| - $('#container').addClass('draging'); |
100 |
| - e.stopPropagation(); |
101 |
| - }); |
| 26 | + var uploader = WebUploader.create({ |
| 27 | + auto: true, |
| 28 | + swf: 'Uploader.swf', |
| 29 | + server: 'https://upload.qbox.me', |
| 30 | + pick: '#pickfiles', |
| 31 | + resize: false, |
| 32 | + dnd: '#container', |
| 33 | + paste: document.body, |
| 34 | + disableGlobalDnd: true, |
| 35 | + accept: [ |
| 36 | + { |
| 37 | + title: 'Images', |
| 38 | + extensions: 'gif,jpg,jpeg,bmp,png', |
| 39 | + mimeTypes: 'image/*' |
| 40 | + } |
| 41 | + ], |
| 42 | + compress: false, |
| 43 | + prepareNextFile: true, |
| 44 | + chunked: true, |
| 45 | + chunkSize: 4194304, |
| 46 | + threads: 5, |
| 47 | + fileNumLimit: 100, |
| 48 | + fileSingleSizeLimit: 10000 * 1024 * 1024, |
| 49 | + duplicate: true, |
| 50 | + formData: { |
| 51 | + token: uptoken |
| 52 | + } |
| 53 | + }) |
| 54 | + uploader.on('startUpload', () => { |
| 55 | + $('.status').css('display', 'flex') |
| 56 | + $('.words').html('上传中...') |
| 57 | + }) |
| 58 | + uploader.on('uploadSuccess', (file, res) => { |
| 59 | + $('.words').html('上传完成') |
| 60 | + setTimeout(() => { |
| 61 | + $('.status').css('display', 'none') |
| 62 | + }, 3000) |
| 63 | + $('#link-area').css('visibility', 'visible') |
| 64 | + console.log('response:', res) |
| 65 | + const link = domain + '/' + res.key |
| 66 | + const markdownLink = `` |
| 67 | + const aLink =`<img src="${link}" />` |
| 68 | + $('#link').val(link) |
| 69 | + $('#markdown').val(markdownLink) |
| 70 | + $('#a-link').val(aLink) |
| 71 | + }) |
| 72 | + uploader.on('uploadError', (file, reason) => { |
| 73 | + if (reason === 'http') { |
| 74 | + reason = '请检查AK/SK等配置正确,然后重新开启本窗口' |
| 75 | + } |
| 76 | + $('.words').html(`上传失败: ${reason}`) |
| 77 | + $('.status').css('display', 'flex') |
| 78 | + $('.status img').css('display', 'none') |
| 79 | + setTimeout(() => { |
| 80 | + $('.status').css('display', 'none') |
| 81 | + $('.status img').css('display', 'unset') |
| 82 | + }, 5000) |
| 83 | + $('#link-area').css('visibility', 'visible') |
| 84 | + }) |
| 85 | + |
| 86 | + $('#container') |
| 87 | + .on('dragenter', function(e) { |
| 88 | + e.preventDefault() |
| 89 | + $('#container').addClass('draging') |
| 90 | + e.stopPropagation() |
| 91 | + }) |
| 92 | + .on('drop', function(e) { |
| 93 | + e.preventDefault() |
| 94 | + $('#container').removeClass('draging') |
| 95 | + e.stopPropagation() |
| 96 | + }) |
| 97 | + .on('dragleave', function(e) { |
| 98 | + e.preventDefault() |
| 99 | + $('#container').removeClass('draging') |
| 100 | + e.stopPropagation() |
| 101 | + }) |
| 102 | + .on('dragover', function(e) { |
| 103 | + e.preventDefault() |
| 104 | + $('#container').addClass('draging') |
| 105 | + e.stopPropagation() |
| 106 | + }) |
102 | 107 |
|
103 |
| - $('#link-area button[data-action=copy]').on('click', function (e) { |
104 |
| - const btn = e.target; |
105 |
| - const targetId = $(btn).attr('data-action-target'); |
106 |
| - const val = $(targetId).val(); |
107 |
| - copyToClipboard(val); |
108 |
| - }); |
| 108 | + $('#link-area button[data-action=copy]').on('click', function(e) { |
| 109 | + const btn = e.target |
| 110 | + const targetId = $(btn).attr('data-action-target') |
| 111 | + const val = $(targetId).val() |
| 112 | + copyToClipboard(val) |
| 113 | + }) |
109 | 114 |
|
110 |
| - function copyToClipboard(input) { |
111 |
| - const el = document.createElement('textarea'); |
112 |
| - el.style.fontsize = '12pt' |
113 |
| - el.style.border = '0' |
114 |
| - el.style.padding = '0' |
115 |
| - el.style.margin = '0' |
116 |
| - el.style.position = 'absolute' |
117 |
| - el.style.left = '-9999px' |
118 |
| - el.setAttribute('readonly', '') |
119 |
| - el.value = input |
| 115 | + function copyToClipboard(input) { |
| 116 | + const el = document.createElement('textarea') |
| 117 | + el.style.fontsize = '12pt' |
| 118 | + el.style.border = '0' |
| 119 | + el.style.padding = '0' |
| 120 | + el.style.margin = '0' |
| 121 | + el.style.position = 'absolute' |
| 122 | + el.style.left = '-9999px' |
| 123 | + el.setAttribute('readonly', '') |
| 124 | + el.value = input |
120 | 125 |
|
121 |
| - document.body.appendChild(el) |
122 |
| - el.select() |
| 126 | + document.body.appendChild(el) |
| 127 | + el.select() |
123 | 128 |
|
124 |
| - let success = false; |
125 |
| - try { |
126 |
| - success = document.execCommand('copy', true); |
127 |
| - } catch (err) { |
128 |
| - } |
| 129 | + let success = false |
| 130 | + try { |
| 131 | + success = document.execCommand('copy', true) |
| 132 | + } catch (err) {} |
129 | 133 |
|
130 |
| - document.body.removeChild(el); |
| 134 | + document.body.removeChild(el) |
131 | 135 |
|
132 |
| - return success; |
133 |
| - } |
134 |
| -}); |
| 136 | + return success |
| 137 | + } |
| 138 | +}) |
0 commit comments