debugViewer.js (5127B)
1 "use strict"; 2 3 var interval = 1000; 4 var intervalID; 5 var stopping = false; 6 var scrolling = false; 7 var autoscroll = true; 8 9 function start() { 10 // If scrolled to the bottom of the page, stay there 11 document.body.onscroll = function (event) { 12 if (!scrolling) { 13 autoscroll = atPageBottom(); 14 } 15 scrolling = false; 16 }; 17 18 updateErrors().then(function () { 19 if (stopping) return; 20 21 addInitialOutput(); 22 Zotero.Debug.addConsoleViewerListener(addLine) 23 intervalID = setInterval(() => updateErrors(), interval); 24 }); 25 } 26 27 function stop() { 28 stopping = true; 29 if (intervalID) { 30 clearInterval(intervalID); 31 intervalID = null; 32 } 33 Zotero.Debug.removeConsoleViewerListener() 34 } 35 36 function updateErrors() { 37 return Zotero.getSystemInfo() 38 .then(function (sysInfo) { 39 if (stopping) return; 40 41 var errors = Zotero.getErrors(true); 42 var errorStr = errors.length ? errors.join('\n\n') + '\n\n' : ''; 43 44 document.getElementById('errors').textContent = errorStr + sysInfo; 45 46 if (autoscroll) { 47 scrollToPageBottom(); 48 } 49 }); 50 } 51 52 function addInitialOutput() { 53 Zotero.Debug.getConsoleViewerOutput().forEach(function (line) { 54 addLine(line); 55 }); 56 } 57 58 function addLine(line) { 59 var p = document.createElement('p'); 60 p.textContent = line; 61 var output = document.getElementById('output'); 62 output.appendChild(p); 63 64 if (autoscroll) { 65 scrollToPageBottom(); 66 } 67 68 document.getElementById('submit-button').removeAttribute('disabled'); 69 document.getElementById('clear-button').removeAttribute('disabled'); 70 } 71 72 function atPageBottom() { 73 return (window.innerHeight + window.scrollY) >= document.body.offsetHeight - 100; 74 } 75 76 function scrollToPageBottom() { 77 // Set a flag when auto-scrolling to differentiate from manual scrolls 78 scrolling = true; 79 window.scrollTo(0, document.body.scrollHeight); 80 } 81 82 function submit(button) { 83 button.setAttribute('disabled', ''); 84 clearSubmitStatus(); 85 86 Components.utils.import("resource://zotero/config.js"); 87 var url = ZOTERO_CONFIG.REPOSITORY_URL + "report?debug=1"; 88 var output = document.getElementById('errors').textContent 89 + "\n\n" + "=========================================================\n\n" 90 + Array.from(document.getElementById('output').childNodes).map(p => p.textContent).join("\n\n"); 91 var pm = document.getElementById('submit-progress'); 92 pm.removeAttribute('hidden'); 93 94 Zotero.HTTP.request( 95 "POST", 96 url, 97 { 98 compressBody: true, 99 body: output, 100 logBodyLength: 30, 101 timeout: 30000, 102 // Update progress meter 103 requestObserver: function (req) { 104 req.channel.notificationCallbacks = { 105 onProgress: function (request, context, progress, progressMax) { 106 if (!pm.value || progress > pm.value) { 107 pm.value = progress; 108 } 109 if (!pm.max || progressMax > pm.max) { 110 pm.max = progressMax; 111 } 112 }, 113 114 // nsIInterfaceRequestor 115 getInterface: function (iid) { 116 try { 117 return this.QueryInterface(iid); 118 } 119 catch (e) { 120 throw Components.results.NS_NOINTERFACE; 121 } 122 }, 123 124 QueryInterface: function(iid) { 125 if (iid.equals(Components.interfaces.nsISupports) || 126 iid.equals(Components.interfaces.nsIInterfaceRequestor) || 127 iid.equals(Components.interfaces.nsIProgressEventSink)) { 128 return this; 129 } 130 throw Components.results.NS_NOINTERFACE; 131 }, 132 133 } 134 } 135 } 136 ) 137 .then(function (xmlhttp) { 138 var reported = xmlhttp.responseXML.getElementsByTagName('reported'); 139 if (reported.length != 1) { 140 showSubmitError(e); 141 return false; 142 } 143 144 showSubmitResult(reported[0].getAttribute('reportID')); 145 }) 146 .catch(function (e) { 147 showSubmitError(e); 148 return false; 149 }) 150 .finally(function () { 151 pm.setAttribute('hidden', ''); 152 button.removeAttribute('disabled'); 153 }); 154 } 155 156 function showSubmitResult(id) { 157 var elem = document.getElementById('submit-result'); 158 elem.removeAttribute('hidden'); 159 document.getElementById('debug-id').textContent = "D" + id; 160 var copyID = document.getElementById('submit-result-copy-id'); 161 copyID.style.visibility = 'visible'; 162 copyID.setAttribute('data-tooltip', 'Copy ID to Clipboard'); 163 } 164 165 function copyIDToClipboard(elem) { 166 var id = document.getElementById('debug-id').textContent; 167 Components.classes["@mozilla.org/widget/clipboardhelper;1"] 168 .getService(Components.interfaces.nsIClipboardHelper) 169 .copyString(id); 170 elem.setAttribute('data-tooltip', 'Copied'); 171 setTimeout(() => elem.style.visibility = 'hidden', 750); 172 } 173 174 function showSubmitError(e) { 175 var elem = document.getElementById('submit-error'); 176 elem.removeAttribute('hidden'); 177 elem.textContent = "Error submitting output"; 178 Components.utils.reportError(e); 179 Zotero.debug(e, 1); 180 } 181 182 function clearSubmitStatus() { 183 document.getElementById('submit-result').setAttribute('hidden', ''); 184 document.getElementById('submit-error').setAttribute('hidden', ''); 185 } 186 187 function clearOutput(button) { 188 document.getElementById('submit-button').setAttribute('disabled', ''); 189 button.setAttribute('disabled', ''); 190 document.getElementById('output').textContent = ''; 191 clearSubmitStatus(); 192 } 193 194 window.addEventListener('load', start); 195 window.addEventListener("unload", stop);