Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ <h1>Export reddit post to markdown format</h1>
<input class="form-check-input" type="radio" name="exportOption" id="treeOption" checked/>
<label class="form-check-label" for="treeOption">Tree style</label>
<input class="form-check-input" type="radio" name="exportOption" id="listOption"/>
<label class="form-check-label" for="exportOption2">List style</label>
<label class="form-check-label" for="listOption">List style</label>
</div>

<label>Export option :</label>
Expand All @@ -59,10 +59,11 @@ <h1>Export reddit post to markdown format</h1>
</div>

<button class="btn btn-primary" onclick="startExport()">Export</button>
<a href="" id="a" download disabled class="btn btn-success">Download markdown file</a>
<div id="ouput-block" hidden>
<a href="" id="downloadButton" download disabled class="btn btn-success">Download markdown file</a>
<button id="copyButton" class="btn btn-success" disabled onclick="copyExport()">Copy to Clipboard</button>
<div id="output-block" hidden>
<h3>Output</h3>
<pre id="ouput-display"></pre>
<pre id="output-display"></pre>
<br/>
</div>
<footer>
Expand Down
45 changes: 29 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const http = new XMLHttpRequest();
var data;
var output = '';
var style = 0;
var escapeNewLine = false;
var spaceComment = false;
let data;
let output = '';
let style = 0;
let escapeNewLine = false;
let spaceComment = false;
var selectedProxy = 'auto';

// CORS Proxy configurations
Expand Down Expand Up @@ -191,11 +191,11 @@ async function fetchData(url) {
comments.forEach(displayComment);

console.log('Done');
var ouput_display = document.getElementById('ouput-display');
var ouput_block = document.getElementById('ouput-block');
ouput_block.removeAttribute('hidden');
ouput_display.innerHTML = output;
download(output, 'output.md', 'text/plain');
let output_display = document.getElementById('output-display');
let output_block = document.getElementById('output-block');
output_block.removeAttribute('hidden');
output_display.innerHTML = output;
download(output_display.textContent, 'output.md', 'text/plain');

} catch (error) {
console.error('Fetch error:', error);
Expand Down Expand Up @@ -242,20 +242,33 @@ function startExport() {
console.log('Start exporting');
setStyle();

var url = getFieldUrl();
let url = getFieldUrl();
if (url) {
fetchData(url);
} else {
console.log('No url provided');
}
}

async function copyExport() {
/*according to https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/writeText
this should only work in 'Secure Contexts' (https or localhost/loopback addresses)
Since GitHub Pages supports https and getting a certificate for any other host is a 10 minute job
I believe it is acceptable to use this despite its restrictions*/
try {
await navigator.clipboard.writeText(document.getElementById('output-display').textContent);
} catch (error) {
console.error('Failed to copy to clipboard:', error.message);
}
}

function download(text, name, type) {
var a = document.getElementById('a');
a.removeAttribute('disabled');
var file = new Blob([text], {type: type});
a.href = URL.createObjectURL(file);
a.download = name;
document.getElementById('copyButton').removeAttribute('disabled');
let download_button = document.getElementById('downloadButton');
download_button.removeAttribute('disabled');
let file = new Blob([text], {type: type});
download_button.href = URL.createObjectURL(file);
download_button.download = name;
}

function displayTitle(post) {
Expand Down