|
9 | 9 | // at 2013-10-29T00:31:54.370Z |
10 | 10 | (function() { |
11 | 11 | try { |
| 12 | + function downloadConsoleHistory() { |
| 13 | + try { |
| 14 | + function alertAndWarn(message) { |
| 15 | + window.alert(message + "\nSee JavaScript console the complete log of warnings"); |
| 16 | + console.warn(message); |
| 17 | + } |
| 18 | + function selectCommand(event, prevent, stop) { |
| 19 | + if (prevent) { |
| 20 | + event.preventDefault(); |
| 21 | + } |
| 22 | + if (stop) { |
| 23 | + event.stopPropagation(); |
| 24 | + } |
| 25 | + var selection = window.getSelection(); |
| 26 | + selection.removeAllRanges(); |
| 27 | + var range = document.createRange(); |
| 28 | + range.selectNodeContents(event.target); |
| 29 | + // Collapse range to end, i.e. toStart argument is false. |
| 30 | + // range.collapse(false); |
| 31 | + // Always add the range to restore focus. |
| 32 | + selection.addRange(range); |
| 33 | + } |
| 34 | + function setupEventListeners(element) { |
| 35 | + element.addEventListener('mouseenter', selectCommand, true); |
| 36 | + element.addEventListener('dragstart', function(event) { |
| 37 | + event.dataTransfer.effectAllowed = "copy"; |
| 38 | + }, false); |
| 39 | + element.addEventListener('drop', function(event) { |
| 40 | + event.dataTransfer.effectAllowed = "none"; |
| 41 | + }, false); |
| 42 | + element.addEventListener('keydown', function(event) { |
| 43 | + selectCommand(event, false, true) |
| 44 | + }, true); |
| 45 | + element.addEventListener('keypress', function(event) { |
| 46 | + selectCommand(event, true, true) |
| 47 | + }, true); |
| 48 | + element.addEventListener('keyup', function(event) { |
| 49 | + selectCommand(event, false, true) |
| 50 | + }, true); |
| 51 | + } |
| 52 | + function installGeneratedHtmlEventListeners() { |
| 53 | + document.addEventListener('readystatechange', function(event) { |
| 54 | + if (event.target.readyState !== 'complete') { |
| 55 | + return; |
| 56 | + } |
| 57 | + (function() { |
| 58 | + var nds = document.querySelectorAll('div>pre'); |
| 59 | + for (var i = 0, len = nds.length; i < len; i++) { |
| 60 | + setupEventListeners(nds[i]); |
| 61 | + } |
| 62 | + // TODO Please note the Download button would be confusing in the generated document. |
| 63 | + document.body.removeChild(document.querySelector('.download').parentElement); |
| 64 | + })(); |
| 65 | + }, false); |
| 66 | + } |
| 67 | + var w = window.open("", ""); |
| 68 | +// var w = window.open("", "dtie_ch", "width=" + window.screen.availWidth / 2 + ",height=" + window.screen.availHeight / 2); |
| 69 | +// var w = window; |
| 70 | + var meta = document.createElement('meta'); |
| 71 | + meta.content = "text/html; charset=UTF-8"; |
| 72 | + meta.httpEquiv = "content-type"; |
| 73 | + w.document.head.appendChild(meta); |
| 74 | + var script = document.createElement('script'); |
| 75 | + // TODO Why the \/ (see https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement)? |
| 76 | + script.type = "text\/javascript"; |
| 77 | + script.textContent = '"use strict"' + ';\n' + selectCommand + ';\n' + setupEventListeners + ';\n' + installGeneratedHtmlEventListeners + ';\n' + 'installGeneratedHtmlEventListeners();\n'; |
| 78 | + w.document.head.appendChild(script); |
| 79 | + var style = document.createElement('style'); |
| 80 | + style.type="text/css"; |
| 81 | + style.textContent = 'pre:before { content: attr(line-number); float: left; text-align: right; width: 2em; padding-right: 1em; }\n'; |
| 82 | + w.document.head.appendChild(style); |
| 83 | + var now = new Date(); |
| 84 | + var ch = JSON.parse(localStorage.consoleHistory); |
| 85 | + w.document.title = "consoleHistory (" + ch.length + ' commands)'; |
| 86 | + var consoleHistoryDownloadButton = document.createElement('input'); |
| 87 | + consoleHistoryDownloadButton.type = 'button'; |
| 88 | + consoleHistoryDownloadButton.className = 'download'; |
| 89 | + consoleHistoryDownloadButton.value = 'Download consoleHistory'; |
| 90 | + w.document.body.appendChild(document.createElement('div').appendChild(document.createTextNode(w.document.title + ' as of ' + now + ' in ' + navigator.userAgent)).parentElement); |
| 91 | + w.document.body.appendChild(document.createElement('div').appendChild(consoleHistoryDownloadButton).parentElement); |
| 92 | + ch.forEach(function(command, index) { |
| 93 | + var pre = document.createElement('pre'); |
| 94 | +// pre.setAttribute('style', 'display: inline; border: 1px solid silver; margin: 1em;'); |
| 95 | + pre.setAttribute('style', 'margin: 0em;'); |
| 96 | + var div = document.createElement('div'); |
| 97 | + div.setAttribute('style', 'margin: 0.25em;'); |
| 98 | + pre.setAttribute('line-number', index + 1); |
| 99 | + pre.contentEditable = true; |
| 100 | + setupEventListeners(pre); |
| 101 | + pre.innerText = command; |
| 102 | + w.document.body.appendChild(div.appendChild(pre).parentElement); |
| 103 | +// w.document.body.appendChild(pre); |
| 104 | + console.log(command); |
| 105 | + }); |
| 106 | + consoleHistoryDownloadButton.addEventListener('click', function(event) { |
| 107 | + var consoleHistoryDocumentBlob = new window.Blob([ |
| 108 | + w.document.documentElement.innerHTML], { |
| 109 | + 'type': 'text/utf-8' |
| 110 | + }); |
| 111 | + var a = document.createElement('a'); |
| 112 | + a.href = URL.createObjectURL(consoleHistoryDocumentBlob); |
| 113 | + a.download = "consoleHistory_" + (now).toJSON().replace(/:/g, '') + '.html'; |
| 114 | + a.click(); |
| 115 | + }, false); |
| 116 | + } catch (exception) { |
| 117 | + alertAndWarn(exception.stack.replace(/:(\d+):(\d+)/g, "$& (Line $1, Column $2)")); |
| 118 | + } |
| 119 | +} |
12 | 120 | var title = 'DevTools Import/Export'; |
13 | 121 | var cancel = 'Cancel'; |
14 | 122 | var alertAndWarn = function(message) { |
|
36 | 144 | div.appendChild(document.createElement('div').appendChild(localStorageDownloadButton).parentElement); |
37 | 145 | var aYes = document.createElement('a'); |
38 | 146 | var aNo = document.createElement('a'); |
39 | | - aYes.innerText = 'Open' + title; |
| 147 | + aYes.innerText = 'Open ' + title; |
40 | 148 | aNo.innerText = cancel; |
41 | 149 | aYes.href = ''; |
42 | 150 | aNo.href = ''; |
| 151 | + aYes.target = 'dtie_dt'; |
| 152 | + aNo.target = ''; |
43 | 153 | var confirmerDiv = document.createElement('div'); |
44 | 154 | confirmerDiv.appendChild(document.createElement('div').appendChild(aYes).parentElement); |
45 | 155 | confirmerDiv.appendChild(document.createElement('div').appendChild(aNo).parentElement); |
46 | 156 | confirmerDiv.setAttribute('style', 'position:fixed; top: 25%; left: 25%; width: 50%; height: 50%; padding: 1em; border: 0.2em solid silver; background: white; font-size: large; z-index: 100;'); |
47 | 157 | if (location.origin === "chrome-devtools://devtools") { |
48 | | - var wa = window.open("", "", "width=300,height=50"); |
| 158 | + var wa = window.open("", "", "width=300,height=50,left=" + window.screen.availWidth / 2 + ",top=" + window.screen.availHeight / 2); |
49 | 159 | wa.document.title = title; |
| 160 | +// window.alert("Please confirm " + title + " to access data for " + location.origin); |
50 | 161 | var confirmerDivPopup = wa.document.createElement('div'); |
51 | 162 | confirmerDivPopup.appendChild(document.createElement('div').appendChild(aYes).parentElement); |
52 | 163 | confirmerDivPopup.appendChild(document.createElement('div').appendChild(aNo).parentElement); |
53 | 164 | wa.document.body.appendChild(confirmerDivPopup); |
54 | 165 | } else { |
55 | | - document.body.appendChild(confirmerDiv); |
| 166 | +// if (document.hasFocus()) { |
| 167 | + document.body.appendChild(confirmerDiv); |
| 168 | +// div.appendChild(document.createTextNode('To import/export devtools source snippets:\nUndock Developer tools into separate window\nand press Ctrl+Shift+I to inspect it.\nThen run this snippet from there.')); |
| 169 | + window.alert('To import/export devtools source snippets:\nUndock Developer tools into separate window\nand press Ctrl+Shift+I to inspect it.\nThen run this snippet from there.'); |
| 170 | +// } else { |
| 171 | +// alertAndWarn("Select tab for " + location.href); |
| 172 | +// } |
56 | 173 | } |
57 | 174 | var removeConfirmer = function(confirmerDiv) { |
58 | 175 | document.body.removeChild(confirmerDiv); |
|
75 | 192 | } |
76 | 193 | }, false); |
77 | 194 | var devtoolsImportExport = function() { |
78 | | - var w = window.open("", "dtie_dt"); |
| 195 | + var w = window.open("", ""); |
| 196 | +// try { |
| 197 | +// window.open("chrome://settings/search#download"); |
| 198 | +// } catch (exception) { |
| 199 | +// alertAndWarn(exception.stack); |
| 200 | +// } |
| 201 | +// var w = window.open("", "dtie_dt"); |
79 | 202 | // var w = window.open("", "_blank", "width=400,height=500,top=100,left=100"); |
80 | 203 | w.document.title = title; |
81 | 204 | w.document.body.appendChild(document.createElement('div').appendChild(div).parentElement); |
82 | 205 | div.appendChild(document.createElement('hr')); |
83 | 206 | // settings.location.href = "chrome://extensions"; |
84 | 207 | if (location.origin === "chrome-devtools://devtools") { |
| 208 | + if (localStorage.consoleHistory) { |
| 209 | + var aHistory = document.createElement('a'); |
| 210 | + aHistory.href = ''; |
| 211 | + aHistory.target = 'dtie_ch'; |
| 212 | + aHistory.innerText = 'devtools console history'; |
| 213 | + var aHistoryDiv = document.createElement('div'); |
| 214 | + div.appendChild(aHistoryDiv); |
| 215 | + aHistoryDiv.appendChild(aHistory); |
| 216 | + aHistoryDiv.addEventListener('click', function(event) { |
| 217 | + event.preventDefault(); |
| 218 | + downloadConsoleHistory(); |
| 219 | + }, false); |
| 220 | + div.appendChild(document.createElement('hr')); |
| 221 | + } |
85 | 222 | var importSnippets = document.createElement('input'); |
86 | 223 | importSnippets.type = 'file'; |
87 | 224 | importSnippets.multiple = true; |
|
92 | 229 | exportButton.value = 'Export All ' + snippets.length + ' Snippets'; |
93 | 230 |
|
94 | 231 | // div.appendChild(document.createElement('div').appendChild(document.createTextNode('Set Google Chrome to ask for download location to avaid malware warning')).parentElement); |
95 | | - var aa = document.createElement('div'); |
96 | | - aa.innerHTML = 'Set Google Chrome to ask for download location (visit <a href="chrome:settings/search#download">chrome:settings/search#download</a>) to avoid malware warning'; |
97 | | - div.appendChild(aa); |
98 | 232 | div.appendChild(document.createElement('div').appendChild(document.createTextNode('Import Snippets')).parentElement); |
99 | 233 | div.appendChild(importSnippets); |
100 | 234 | div.appendChild(document.createElement('hr')); |
101 | 235 | div.appendChild(document.createElement('div').appendChild(exportButton).parentElement); |
| 236 | + var aa = document.createElement('div'); |
| 237 | + aa.innerHTML = 'Set Google Chrome to ask for download location (visit <a href="chrome://settings/search#download">chrome://settings/search#download</a>) to avoid malware warning'; |
| 238 | + div.appendChild(aa); |
102 | 239 | div.appendChild(document.createElement('div').appendChild(document.createTextNode('Export Snippets Individually')).parentElement); |
103 | 240 | snippets.forEach(function(snippet) { |
104 | 241 | var blob = new window.Blob(['// snippet ' + snippet.name + ' exported by snippeteer from\n// ' + navigator.userAgent + '\n// at ' + (new Date()).toJSON() + '\n' + snippet.content], { |
|
219 | 356 | w.document.body.classList.remove('valid'); |
220 | 357 | } |
221 | 358 | }, false && "useCapture"); //$NON-NLS-0$ //$NON-NLS-1$ |
222 | | - } else { |
223 | | - div.appendChild(document.createTextNode('Undock Developer tools into separate window and press Ctrl+Shift+I to inspect it. Then run snipetter from there to import/export snippets.')); |
224 | 359 | } |
| 360 | +// else { |
| 361 | +// div.appendChild(document.createTextNode('Undock Developer tools into separate window and press Ctrl+Shift+I to inspect it. Then run snipetter from there to import/export snippets.')); |
| 362 | +// } |
225 | 363 | } |
226 | 364 | // aYes.click(); |
227 | 365 | } catch (exception) { |
|
0 commit comments