Last active
June 26, 2019 19:03
-
-
Save johnholbrook/77edfd56cd54e65acbd4877e3154458a to your computer and use it in GitHub Desktop.
Tampermonkey script to add 'view post source' button to Discourse forum posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Discourse View Post Source | |
| // @namespace jholbrook | |
| // @author John Holbrook | |
| // @version 0.1 | |
| // @description Adds a 'view post source' button to posts on Discourse forums | |
| // @website https://gist.github.com/dhmmjoph/77edfd56cd54e65acbd4877e3154458a | |
| //Change these or add aditional sites here: | |
| // @match https://www.vexforum.com/t/* | |
| // @match http://www.vexforum.com/t/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| // Get the current thread ID | |
| var this_page_url_contents = window.location.href.split('/'); | |
| var this_thread_id = this_page_url_contents[5]; | |
| // Get all the post objects | |
| var all_posts = document.getElementsByClassName("topic-post"); | |
| // For each post... | |
| for (var i=0; i<all_posts.length; i++){ | |
| // Find the area with the controls at the bottom of the post | |
| let actions_div = all_posts[i].getElementsByTagName("article")[0]. | |
| getElementsByClassName("row")[0]. | |
| getElementsByClassName("topic-body")[0]. | |
| getElementsByClassName("contents")[0]. | |
| getElementsByClassName("post-menu-area")[0]. | |
| getElementsByClassName("post-controls")[0]. | |
| getElementsByClassName("actions")[0]; | |
| //console.log(actions_div); | |
| // Build the URL of the raw post text | |
| var source_url = "https://www.vexforum.com/raw/" + this_thread_id + "/" + (i+1); | |
| // Construct the new button and add it to the row of existing buttons | |
| var new_button_html = "<div class=\"double-button\" aria-label=\"View post source\" style=\"font-family:\'Lucida Console\', Monaco, monospace; font-size=1.5em;\" text=\"View Post Source\">"; | |
| new_button_html += "<a target=\"_blank\" href=\"" + source_url + "\">"; | |
| new_button_html += "</>"; | |
| new_button_html += "</a></div>" | |
| actions_div.innerHTML = new_button_html + actions_div.innerHTML; | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment