-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcc-forums-raw-viewer.user.js
More file actions
85 lines (72 loc) · 2.8 KB
/
cc-forums-raw-viewer.user.js
File metadata and controls
85 lines (72 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// ==UserScript==
// @name Unformatted Forum Post Viewer (Codecademy Discourse)
// @namespace https://github.com/selectlearns
// @version 1.1
// @description Adds a button to see the unformatted (raw) post
// @author selectlearns (SelectAll in forums)
// @match https://discuss.codecademy.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
const handleRawClick = async (postId, topicId, postNumber) => {
const post = document.querySelector(`article[data-post-id='${postId}']`).querySelector('div.cooked');
const rawUrl = `https://discuss.codecademy.com/raw/${topicId}/${postNumber}`;
let text;
try {
const response = await fetch(rawUrl);
if (response.ok) {
text = await response.text();
} else {
text = `Unable to load.\n\nGo to:\n${rawUrl}`;
}
} catch (error) {
text = `Network error encountered.\n\nGo to:\n${rawUrl}`;
}
const pos = post.getBoundingClientRect();
showRawView(text, pos.left + window.scrollX, pos.top + window.scrollY, post.clientWidth, post.clientHeight);
};
const showRawView = (text, left, top, width, height) => {
const rawView = document.getElementById('raw-view') || insertRawView();
rawView.querySelector('#raw-text').value = text;
rawView.style.left = `${left}px`;
rawView.style.top = `${top}px`;
rawView.style.width = `${width}px`;
rawView.style.height = `${height}px`;
rawView.style.display = 'block';
document.body.addEventListener('click', () => rawView.style.display = 'none', { once: true });
};
const insertRawView = () => {
const styles = {
position: 'absolute',
zIndex: 300,
boxShadow: '0 4px 14px rgb(0 0 0 / 15%)',
backgroundColor: 'var(--secondary)'
};
const div = document.createElement('div');
div.setAttribute('id', 'raw-view');
div.setAttribute('class', 'ember-view');
for (const style in styles) {
div.style[style] = styles[style];
}
div.innerHTML = `<textarea id="raw-text" readonly spellcheck="false" style="width: 100%; height: 100%;" class="d-editor-input ember-text-area ember-view"></textarea>`;
div.querySelector('#raw-text').onclick = (event) => event.stopPropagation();
document.getElementById('share-link').after(div);
return div;
};
const withPluginApi = require('discourse/lib/plugin-api').withPluginApi;
withPluginApi('0.8', api => {
api.attachWidgetAction('post', 'rawButton', function () {
handleRawClick(this.attrs.id, this.attrs.topicId, this.attrs.post_number);
});
api.addPostMenuButton('raw', () => {
return {
action: 'rawButton',
icon: 'code',
className: 'raw-button',
title: 'get the raw text of this post',
position: 'second-last-hidden'
};
});
});
})();