confirm.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var M=$('#msg'),I=$('#bInstall'),U=$('#url'),B=$('#bClose'),C=$('#cClose'),data={},T;
  2. function showMsg(m,t){M.innerHTML=m;M.setAttribute('title',t||m);}
  3. B.onclick=function(){window.close();};
  4. C.onchange=function(){
  5. chrome.runtime.sendMessage({cmd:'SetOption',data:{key:'closeAfterInstall',value:C.checked}});
  6. };
  7. I.onclick=function(){
  8. chrome.runtime.sendMessage({
  9. cmd:'ParseScript',
  10. data:{
  11. url:data.url,
  12. from:data.from,
  13. code:T.getValue(),
  14. require:data.require,
  15. resources:data.resources,
  16. },
  17. });
  18. I.disabled=true;
  19. };
  20. chrome.runtime.onMessage.addListener(function(req,src,callback) {
  21. var maps={
  22. ShowMessage: function(o){
  23. showMsg(o.message);
  24. if(callback) callback();
  25. if(o.status>=0&&C.checked) window.close();
  26. },
  27. },f=maps[req.cmd];
  28. if(f) f(req.data,src,callback);
  29. return true;
  30. });
  31. chrome.runtime.sendMessage({cmd:'GetOption',data:'closeAfterInstall'},function(o){C.checked=!!o;});
  32. initEditor(function(o){
  33. T=o;o=location.search.slice(1);
  34. o.split('&').forEach(function(i){
  35. i.replace(/^([^=]*)=(.*)$/,function(r,g1,g2){data[g1]=decodeURIComponent(g2);});
  36. });
  37. U.innerHTML=_('msgScriptURL',[data.url||'-']);
  38. function error(){showMsg(_('msgErrorLoadingData'));}
  39. function loaded(){showMsg(_('msgLoadedData'));I.disabled=false;}
  40. if(data.url) {
  41. U.setAttribute('title',data.url);
  42. showMsg(_('msgLoadingData'));
  43. var x=new XMLHttpRequest();
  44. x.open('GET',data.url,true);
  45. x.onloadend=function(){
  46. if((!this.status||this.status==200)&&this.responseText) {
  47. T.setValueAndFocus(this.responseText);
  48. chrome.runtime.sendMessage({cmd:'ParseMeta',data:this.responseText},function(o){
  49. function next() {
  50. i++;
  51. if(i>=l) {
  52. if(err.length) showMsg(_('msgErrorLoadingDependency'),err.join('\n'));
  53. else loaded();
  54. } else showMsg(_('msgLoadingDependency',[i,l]));
  55. }
  56. function loadDependency(d,r,b) {
  57. r.forEach(function(u){
  58. var x=new XMLHttpRequest();
  59. x.open('GET',u,true);
  60. if(b) x.responseType='blob';
  61. x.onloadend=function(){
  62. if(this.status==200) {
  63. if(b) {
  64. var r=new FileReader();
  65. r.onload=function(e){
  66. d[u]=window.btoa(r.result);
  67. next();
  68. };
  69. r.readAsBinaryString(this.response);
  70. return;
  71. } else d[u]=this.responseText;
  72. } else err.push(u);
  73. next();
  74. };
  75. x.send();
  76. });
  77. }
  78. var i=0,l,err=[],u=[];
  79. for(l in o.resources) u.push(o.resources[l]);
  80. l=o.require.length+u.length;
  81. if(l) {
  82. showMsg(_('msgLoadingDependency',[i,l]));
  83. data.require={};
  84. loadDependency(data.require,o.require);
  85. data.resources={};
  86. loadDependency(data.resources,u,true);
  87. } else loaded();
  88. });
  89. } else error();
  90. };
  91. x.send();
  92. } else error();
  93. },{exit:B.onclick,readonly:true});
  94. initI18n();