Skip to content
Closed
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
29 changes: 22 additions & 7 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const http = new XMLHttpRequest();

var data;
var output = '';
var style = 0;
Expand Down Expand Up @@ -191,11 +192,12 @@ 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');
download(document.getElementById('output-display').textContent, 'output.md', 'text/plain');

} catch (error) {
console.error('Fetch error:', error);
Expand Down Expand Up @@ -242,20 +244,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 acceptible to use this despite it's restrictions*/
try {
await navigator.clipboard.writeText(document.getElementById('output-display').textContent);
} catch (error) {
console.error(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