Skip to content

Instantly share code, notes, and snippets.

@ichisadashioko
Created April 4, 2023 20:40
Show Gist options
  • Save ichisadashioko/bc156118414cb5afd9ec44b2cf93549b to your computer and use it in GitHub Desktop.
Save ichisadashioko/bc156118414cb5afd9ec44b2cf93549b to your computer and use it in GitHub Desktop.
download current rendered html page for processing later
let current_time = new Date();
let year_str = current_time.getUTCFullYear().toString();
while (year_str.length < 4) { year_str = '0' + year_str; }
let month_str = (current_time.getUTCMonth() + 1).toString();
while (month_str.length < 2) { month_str = '0' + month_str; }
let day_str = current_time.getUTCDate().toString();
while (day_str.length < 2) { day_str = '0' + day_str; }
let hour_str = current_time.getUTCHours().toString();
while (hour_str.length < 2) { hour_str = '0' + hour_str; }
let minute_str = current_time.getUTCMinutes().toString();
while (minute_str.length < 2) { minute_str = '0' + minute_str; }
let second_str = current_time.getUTCSeconds().toString();
while (second_str.length < 2) { second_str = '0' + second_str; }
let millisecond_str = current_time.getUTCMilliseconds().toString();
while (millisecond_str.length < 3) { millisecond_str = '0' + millisecond_str; }
let output_filename = `html-${year_str}${month_str}${day_str}-${hour_str}${minute_str}${second_str}.${millisecond_str}.json`;
let html_string = document.documentElement.outerHTML;
let current_url = window.location.href;
let output_obj = {
'url': current_url,
'html': html_string,
};
let output_json = JSON.stringify(output_obj, null, '\t');
function download_text_as_file(
filename,
text,
auto_click,
auto_remove,
) {
var element = document.createElement('a');
element.classList.add('archive_aliexpress_html_download_element');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
// element.style.display = 'none';
document.body.prepend(element);
if (auto_click) { element.click(); }
if (auto_remove) { element.remove(); }
// document.body.removeChild(element);
}
download_text_as_file(
output_filename,
output_json,
true,
false,
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment