Skip to content

Commit bdb320b

Browse files
committed
Integrate latest functionality of collect_command_history.js
Drop window.open strWindowFeatures argument to open in tabs instead of popups, still via anchor click listeners.
1 parent e1333f7 commit bdb320b

File tree

1 file changed

+147
-9
lines changed

1 file changed

+147
-9
lines changed

GoogleChromeSnippets/devtools_import_export.js

Lines changed: 147 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,114 @@
99
// at 2013-10-29T00:31:54.370Z
1010
(function() {
1111
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+
}
12120
var title = 'DevTools Import/Export';
13121
var cancel = 'Cancel';
14122
var alertAndWarn = function(message) {
@@ -36,23 +144,32 @@
36144
div.appendChild(document.createElement('div').appendChild(localStorageDownloadButton).parentElement);
37145
var aYes = document.createElement('a');
38146
var aNo = document.createElement('a');
39-
aYes.innerText = 'Open' + title;
147+
aYes.innerText = 'Open ' + title;
40148
aNo.innerText = cancel;
41149
aYes.href = '';
42150
aNo.href = '';
151+
aYes.target = 'dtie_dt';
152+
aNo.target = '';
43153
var confirmerDiv = document.createElement('div');
44154
confirmerDiv.appendChild(document.createElement('div').appendChild(aYes).parentElement);
45155
confirmerDiv.appendChild(document.createElement('div').appendChild(aNo).parentElement);
46156
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;');
47157
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);
49159
wa.document.title = title;
160+
// window.alert("Please confirm " + title + " to access data for " + location.origin);
50161
var confirmerDivPopup = wa.document.createElement('div');
51162
confirmerDivPopup.appendChild(document.createElement('div').appendChild(aYes).parentElement);
52163
confirmerDivPopup.appendChild(document.createElement('div').appendChild(aNo).parentElement);
53164
wa.document.body.appendChild(confirmerDivPopup);
54165
} 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+
// }
56173
}
57174
var removeConfirmer = function(confirmerDiv) {
58175
document.body.removeChild(confirmerDiv);
@@ -75,13 +192,33 @@
75192
}
76193
}, false);
77194
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");
79202
// var w = window.open("", "_blank", "width=400,height=500,top=100,left=100");
80203
w.document.title = title;
81204
w.document.body.appendChild(document.createElement('div').appendChild(div).parentElement);
82205
div.appendChild(document.createElement('hr'));
83206
// settings.location.href = "chrome://extensions";
84207
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+
}
85222
var importSnippets = document.createElement('input');
86223
importSnippets.type = 'file';
87224
importSnippets.multiple = true;
@@ -92,13 +229,13 @@
92229
exportButton.value = 'Export All ' + snippets.length + ' Snippets';
93230

94231
// 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);
98232
div.appendChild(document.createElement('div').appendChild(document.createTextNode('Import Snippets')).parentElement);
99233
div.appendChild(importSnippets);
100234
div.appendChild(document.createElement('hr'));
101235
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);
102239
div.appendChild(document.createElement('div').appendChild(document.createTextNode('Export Snippets Individually')).parentElement);
103240
snippets.forEach(function(snippet) {
104241
var blob = new window.Blob(['// snippet ' + snippet.name + ' exported by snippeteer from\n// ' + navigator.userAgent + '\n// at ' + (new Date()).toJSON() + '\n' + snippet.content], {
@@ -219,9 +356,10 @@
219356
w.document.body.classList.remove('valid');
220357
}
221358
}, 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.'));
224359
}
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+
// }
225363
}
226364
// aYes.click();
227365
} catch (exception) {

0 commit comments

Comments
 (0)