Skip to content

Commit

Permalink
Optimize sequence of checking hash on initial load
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Dec 12, 2019
1 parent c23b3e8 commit fddaad4
Showing 1 changed file with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,21 @@ function EditorUI() {
return qs.parse(queryString || '');
};

// Support for loading a console snippet from a remote source, like support docs.
const onHashChange = debounce(() => {
const { load_from: sourceLocation } = readQueryParams();
if (!sourceLocation) {
return;
}
if (/^https?:\/\//.test(sourceLocation)) {
const loadBufferFromRemote = (url: string) => {
if (/^https?:\/\//.test(url)) {
const loadFrom: Record<string, any> = {
url: sourceLocation,
url,
// Having dataType here is required as it doesn't allow jQuery to `eval` content
// coming from the external source thereby preventing XSS attack.
dataType: 'text',
kbnXsrfToken: false,
};

if (/https?:\/\/api\.github\.com/.test(sourceLocation)) {
if (/https?:\/\/api\.github\.com/.test(url)) {
loadFrom.headers = { Accept: 'application/vnd.github.v3.raw' };
}

// Fire and forget.
$.ajax(loadFrom).done(async data => {
const coreEditor = editor.getCoreEditor();
await editor.update(data, true);
Expand All @@ -120,12 +116,21 @@ function EditorUI() {
coreEditor.getContainer().focus();
});
}
};

// Support for loading a console snippet from a remote source, like support docs.
const onHashChange = debounce(() => {
const { load_from: url } = readQueryParams();
if (!url) {
return;
}
loadBufferFromRemote(url);
}, 200);
window.addEventListener('hashchange', onHashChange);

if (readQueryParams().load_from) {
// Do an initial check against the current hash value.
onHashChange();
const initialQueryParams = readQueryParams();
if (initialQueryParams.load_from) {
loadBufferFromRemote(initialQueryParams.load_from);
} else {
const { content: text } = history.getSavedEditorState() || {
content: DEFAULT_INPUT_VALUE,
Expand Down

0 comments on commit fddaad4

Please sign in to comment.