confirm.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. 'use strict';
  2. !function() {
  3. var delay = 2000;
  4. var data = {};
  5. var lbUrl = $('#url');
  6. var lbMsg = $('#msg');
  7. var cbClose = $('#cbClose');
  8. var cbTrack = $('#cbTrack');
  9. var btInstall = $('#bInstall');
  10. var options = $('.options');
  11. var timer, editor;
  12. function showMessage(message, title) {
  13. lbMsg.innerHTML = message;
  14. lbMsg.setAttribute('title', title || message);
  15. }
  16. function zfill(num, length) {
  17. num = num.toString();
  18. while(num.length < length) num = '0' + num;
  19. return num;
  20. }
  21. function getTimeString() {
  22. var now = new Date();
  23. return zfill(now.getHours(), 2) + ':' +
  24. zfill(now.getMinutes(), 2) + ':' +
  25. zfill(now.getSeconds(), 2);
  26. }
  27. function getScriptFile(cb) {
  28. var x = new XMLHttpRequest();
  29. x.open('GET', data.url, true);
  30. x.onloadend = function() {
  31. var x = this;
  32. if(!x.status) data.local = true;
  33. if(x.status && x.status != 200 || !x.responseText)
  34. showMessage(_('msgErrorLoadingData'));
  35. else
  36. cb(x.responseText);
  37. };
  38. x.send();
  39. }
  40. function loadDependency(dict, urls, isBlob, cb) {
  41. urls.forEach(function(url){
  42. if(dict[url]) return cb();
  43. var x = new XMLHttpRequest();
  44. x.open('GET', url, true);
  45. if(isBlob) x.responseType = 'blob';
  46. x.onloadend = function(){
  47. var x = this;
  48. if(!x.status || x.status == 200) {
  49. if(isBlob) {
  50. var r = new FileReader();
  51. r.onload = function(e) {
  52. dict[url] = window.btoa(x.result);
  53. cb();
  54. };
  55. r.readAsBinaryString(x.response);
  56. } else {
  57. dict[url] = x.responseText;
  58. cb();
  59. }
  60. } else cb(url);
  61. };
  62. x.send();
  63. });
  64. }
  65. function parseMeta(body, cb) {
  66. function finish(){
  67. showMessage(_('msgLoadedData'));
  68. btInstall.disabled = false;
  69. data.depStatus = 1;
  70. if(cb) cb();
  71. }
  72. function finishOne(errUrl) {
  73. if(errUrl) err.push(errUrl);
  74. i ++;
  75. if(i >= length) {
  76. if(err.length) {
  77. showMessage(_('msgErrorLoadingDependency'), err.join('\n'));
  78. } else finish();
  79. } else showMessage(_('msgLoadingDependency', [i, length]));
  80. }
  81. var urls = [];
  82. var err = [];
  83. var i;
  84. var length;
  85. data.depStatus=0;
  86. chrome.runtime.sendMessage({cmd: 'ParseMeta', data: body}, function(script) {
  87. for(i in script.resources) urls.push(script.resources[i]);
  88. length = script.require.length + urls.length;
  89. if(length) {
  90. showMessage(_('msgLoadingDependency', [i = 0, length]));
  91. loadDependency(data.require = {}, script.require, false, finishOne);
  92. loadDependency(data.resources = {}, urls, true, finishOne);
  93. } else finish();
  94. });
  95. }
  96. function updateClose(){
  97. if(cbTrack.disabled = cbClose.checked) {
  98. cbTrack.checked = false;
  99. updateLocal();
  100. }
  101. setOption('closeAfterInstall', cbClose.checked);
  102. }
  103. function updateLocal(){
  104. setOption('trackLocalFile', cbTrack.checked);
  105. if(cbTrack.checked && data.depStatus && data.local)
  106. btInstall.disabled=false;
  107. }
  108. function trackLocalFile(){
  109. function check(){
  110. timer=null;
  111. getScriptFile(function(body) {
  112. var oldbody = editor.getValue();
  113. editor.setValue(body);
  114. body = editor.getValue();
  115. if(oldbody != body)
  116. parseMeta(body, function(){
  117. if(cbTrack.checked) install();
  118. });
  119. else if(cbTrack.checked)
  120. timer = setTimeout(check, delay);
  121. });
  122. }
  123. if(!timer) timer = setTimeout(check, delay);
  124. }
  125. function install(){
  126. btInstall.disabled=true;
  127. chrome.runtime.sendMessage({
  128. cmd:'ParseScript',
  129. data:{
  130. url: data.url,
  131. from: data.from,
  132. code: editor.getValue(),
  133. require: data.require,
  134. resources: data.resources,
  135. },
  136. }, function(res){
  137. showMessage(res.message + '[' + getTimeString() + ']');
  138. if(res.code >= 0) {
  139. if(cbClose.checked) close();
  140. else if(data.local && cbTrack.checked) trackLocalFile();
  141. }
  142. });
  143. }
  144. function close() {
  145. window.close();
  146. }
  147. function bindEvents() {
  148. cbClose.checked = getOption('closeAfterInstall');
  149. updateClose();
  150. cbClose.addEventListener('change', updateClose, false);
  151. cbTrack.checked = getOption('trackLocalFile');
  152. cbTrack.addEventListener('change', updateLocal, false);
  153. $('#bClose').addEventListener('click', close, false);
  154. btInstall.addEventListener('click', install, false);
  155. var hideOptions = function(){
  156. options.style.display = '';
  157. document.removeEventListener('mousedown', hideOptions, false);
  158. };
  159. $('#bOptions').addEventListener('click', function(e){
  160. options.style.right = this.parentNode.offsetWidth - this.offsetWidth - this.offsetLeft + 'px';
  161. options.style.display = 'block';
  162. document.addEventListener('mousedown', hideOptions, false);
  163. }, false);
  164. options.addEventListener('mousedown', stopPropagation, false);
  165. }
  166. initEditor({
  167. container: $('.code'),
  168. callback: function(_editor) {
  169. editor = _editor;
  170. location.search.slice(1).split('&').forEach(function(part) {
  171. part.replace(/^([^=]*)=(.*)$/, function(value, group1, group2) {
  172. data[group1] = decodeURIComponent(group2);
  173. });
  174. });
  175. lbUrl.innerHTML = _('msgScriptURL', [data.url || '-']);
  176. if(data.url) {
  177. lbUrl.setAttribute('title', data.url);
  178. showMessage(_('msgLoadingData'));
  179. getScriptFile(function(body) {
  180. editor.setValueAndFocus(body);
  181. parseMeta(body);
  182. });
  183. }
  184. },
  185. onexit: close,
  186. readonly: true,
  187. });
  188. bindEvents();
  189. initI18n();
  190. }();