Advertisement
Guest User

Untitled

a guest
Sep 19th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         New Userscript
  3. // @namespace    https://forums.mst3k.com/*
  4. // @version      0.1
  5. // @description  try to take over the world!
  6. // @author       You
  7. // @match        https://forums.mst3k.com/*
  8. // @icon         https://www.google.com/s2/favicons?domain=mst3k.com
  9. // @grant        all
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14.     'use strict';
  15.  
  16.     const { REPLY } = require('discourse/models/composer').default;
  17.     const composer = Discourse.__container__.lookup('controller:composer');
  18.  
  19.  
  20.     var pageType;
  21.     var pageId;
  22.     var topicPost;
  23.  
  24.     var postStream;
  25.     var streamIsEmpty = false;
  26.  
  27.     var posts;
  28.     var postContainer;
  29.     var loadedPosts = [];
  30.  
  31.     var insertedPosts = [];
  32.  
  33.     var csrf;
  34.  
  35.     setInterval(
  36.         function(){
  37.             if ((window.location.pathname.split("/")[1] == "t") && !(window.location.pathname.split("/")[3] == pageId)){
  38.                 if ( typeof window.location.pathname.split("/")[4] !== "undefined" ){
  39.                     location.href = "/t/" + window.location.pathname.split("/")[3];
  40.                     return;
  41.                 }
  42.                 pageType = window.location.pathname.split("/")[1];
  43.                 pageId   = window.location.pathname.split("/")[3];
  44.  
  45.                 csrf = document.getElementsByName("csrf-token")[0].content;
  46.  
  47.                 loadPostJson();
  48.             }
  49.             if ( !(window.location.pathname.split("/")[1] == "t") ){
  50.                 pageType = window.location.pathname.split("/")[1];
  51.                 pageId   = null;
  52.             }
  53.         }
  54.     ,1000);
  55.  
  56.     function loadPostJson(){
  57.         /*
  58.         fetch( window.location.pathname + ".json" )
  59.             .then(response => response.json())
  60.             .then(data => createThreadedView(data));
  61.             */
  62.         var topic = Discourse.__container__.lookup("controller:topic").get("model");
  63.         postStream = topic.postStream.stream;
  64.  
  65.         createThreadedView(topic);
  66.     }
  67.  
  68.     function createThreadedView(data){
  69.         console.log(data);
  70.         postContainer = document.querySelector(".container.posts");
  71.         postContainer.style.display = "block";
  72.         posts = data.postStream.posts;
  73.  
  74.         postContainer.innerHTML = `
  75.         <STYLE>
  76.         .container.posts {
  77.         }
  78.         .threaded-post {
  79.             display:block;
  80.             box-sizing:border-box;
  81.             width:100%;
  82.             margin:0px;
  83.             margin-top:10px;
  84.             padding:10px;
  85.             background:rgba(128,128,128,0.05);
  86.             border-radius:8px;
  87.             box-shadow:0px 2px 8px rgba(0,0,0,0.33);
  88.         }
  89.         .threaded-avatar {
  90.             border-radius:50%;
  91.         }
  92.         .threaded-username {
  93.             font-size:larger;
  94.             font-weight:600;
  95.             margin:10px;
  96.         }
  97.         .threaded-user-title, .threaded-post-date {
  98.             color:#888;
  99.         }
  100.         .threaded-post-date {
  101.             float:right;
  102.         }
  103.         .threaded-collapse-toggle {
  104.             float:right;
  105.             margin:5px;
  106.             width:16px;
  107.             height:16px;
  108.             line-height:8px;
  109.             text-align:center;
  110.         }
  111.         .threaded-post.collapsed > * {
  112.            display:none;
  113.         }
  114.         .threaded-post.collapsed > .threaded-post-header {
  115.            display:block;
  116.            font-size:smaller;
  117.            color:#888;
  118.         }
  119.         .threaded-post.collapsed > .threaded-post-header > img {
  120.            display:none;
  121.         }
  122.         .post-action-zone > * {
  123.            margin-right:10px;
  124.         }
  125.         .threaded-post-liked {
  126.            background: #ff8888;
  127.            border:1px solid #888;
  128.            border-radius:4px;
  129.         }
  130.         </STYLE>`;
  131.  
  132.         for ( var i = 0; i < posts.length; i++ ){
  133.             createPostElement(posts[i], i);
  134.  
  135.             insertedPosts[ posts[i].post_number ] = posts[i];
  136.  
  137.             var parentPost = insertedPosts[ posts[i].reply_to_post_number ];
  138.             var parentElement = postContainer;
  139.  
  140.             if ( parentPost ){
  141.                 parentElement = parentPost.repliesElement;
  142.             } else {
  143.                 if ( i > 0 ){ parentElement = posts[0].repliesElement; }
  144.             }
  145.             console.log(posts[i].element);
  146.             parentElement.appendChild( posts[i].element );
  147.         }
  148.  
  149.         if ( postStream.length > 0 ){
  150.             console.log("*******************");
  151.             console.log("**** FETCHING *****");
  152.             console.log("*******************");
  153.             loadMorePosts();
  154.         }
  155.  
  156.     }
  157.  
  158.     function loadMorePosts(){
  159.         var url = "/t/"+pageId+"/posts.json?";
  160.         for ( var i = 0; i < 20; i++ ){
  161.             if ( postStream[i] ){
  162.                 if ( i !== 0 ){ url = url + "&"; }
  163.                 url = url + "post_ids[]="+postStream[i];
  164.             }
  165.         }
  166.         console.log(url);
  167.         fetch( url ).then(response => response.json()).then( function(data){
  168.             addThreadedPosts(data.post_stream.posts);
  169.         });
  170.     }
  171.  
  172.     function addThreadedPosts(newPosts){
  173.         var start = posts.length;
  174.         posts = posts.concat(newPosts);
  175.  
  176.         console.log(posts);
  177.  
  178.         for ( var i = start; i < posts.length; i++ ){
  179.             createPostElement(posts[i], i);
  180.  
  181.             insertedPosts[ posts[i].post_number ] = posts[i];
  182.  
  183.             var parentPost = insertedPosts[ posts[i].reply_to_post_number ];
  184.             var parentElement = postContainer;
  185.  
  186.             if ( parentPost ){
  187.                 parentElement = parentPost.repliesElement;
  188.             } else {
  189.                 if ( i > 0 ){ parentElement = posts[0].repliesElement; }
  190.             }
  191. console.log("parent:");
  192.             console.log(parentPost);
  193. console.log("child:");
  194.             console.log(posts[i]);
  195.             parentElement.appendChild( posts[i].element );
  196.         }
  197.  
  198.         if ( postStream.length > 0 ){
  199.             console.log("*******************");
  200.             console.log("**** FETCHING *****");
  201.             console.log("*******************");
  202.             loadMorePosts();
  203.         }
  204.     }
  205.  
  206.     function createPostElement(post, postId){
  207.         var postDate = new Date(post.created_at);
  208.  
  209.         postStream = postStream.remove( post.id );
  210.  
  211.         post.element;
  212.         post.element = document.createElement("div");
  213.         post.element.setAttribute("id","post-"+postId);
  214.         post.element.setAttribute("class","threaded-post");
  215.         post.element.setAttribute("data-id",post.id);
  216.  
  217.         post.headerElement;
  218.         post.headerElement = document.createElement("div");
  219.         post.headerElement.setAttribute("id","post-header-"+postId);
  220.         post.headerElement.setAttribute("class","threaded-post-header");
  221.         post.headerElement.innerHTML = `
  222.         <img src="`+post.avatar_template.replace("{size}","40")+`" class="threaded-avatar">
  223.         <span class="threaded-username">`+post.username+`</span>
  224.         <span class="threaded-user-title">`+post.user_title+`</span>
  225.         `;
  226.  
  227.         post.dateTime = document.createElement("span");
  228.         post.dateTime.setAttribute("class","threaded-post-date");
  229.         post.dateTime.innerText = postDate.toLocaleDateString()+" "+postDate.toLocaleTimeString();
  230.  
  231.         post.collapse = document.createElement("button");
  232.         post.collapse.setAttribute("class","threaded-collapse-toggle");
  233.         post.collapse.innerText = "-";
  234.         post.collapse.addEventListener("click",toggleCollapse);
  235.  
  236.         post.headerElement.appendChild(post.collapse);
  237.         post.headerElement.appendChild(post.dateTime);
  238.  
  239.         post.bodyElement;
  240.         post.bodyElement = document.createElement("div");
  241.         post.bodyElement.setAttribute("id","post-body-"+postId);
  242.         post.bodyElement.setAttribute("class","threaded-post-body");
  243.         post.bodyElement.innerHTML = post.cooked;
  244.  
  245.         post.repliesElement;
  246.         post.repliesElement = document.createElement("div");
  247.         post.repliesElement.setAttribute("id","post-replies-"+postId);
  248.         post.repliesElement.setAttribute("class","threaded-post-replies");
  249.  
  250.         post.actionZone;
  251.         post.actionZone = document.createElement("div");
  252.         post.actionZone.setAttribute("id","post-action-zone-"+postId);
  253.         post.actionZone.setAttribute("class","post-action-zone");
  254.  
  255.  
  256.         post.replyButton;
  257.         post.replyButton = document.createElement("button");
  258.         post.replyButton.setAttribute("id","post-reply-button-"+postId);
  259.         post.replyButton.setAttribute("class","threaded-post-reply-button");
  260.         post.replyButton.innerText = "Reply";
  261.         post.replyButton.addEventListener("click",function(){
  262.             composer.open({
  263.                 action: REPLY,
  264.                 draftKey: 'post_'+post.id,
  265.                 post
  266.             })
  267.         });
  268.  
  269.             post.likeButton;
  270.             post.likeButton = document.createElement("button");
  271.             post.likeButton.setAttribute("id","post-reply-button-"+postId);
  272.             post.likeButton.setAttribute("class","threaded-post-reply-button threaded-post-action-button");
  273.             post.likeButton.innerText = "Like";
  274.  
  275.             if ( post.actions_summary[0].hasOwnProperty("acted") ){
  276.                 if ( post.actions_summary[0].acted ){
  277.                     post.likeButton.classList.add("threaded-post-liked");
  278.                 }
  279.             }
  280.  
  281.             post.likeButton.addEventListener("click",function(){
  282.  
  283.                 postData( "/post_actions", "id="+post.id+"&post_action_type_id=2&flag_topic=false" );
  284.             });
  285.  
  286.             post.likeCounter;
  287.             post.likeCounter = document.createElement("span");
  288.             post.likeCounter.setAttribute("id","post-like-counter-"+postId);
  289.             post.likeCounter.setAttribute("class","post-like-counter");
  290.             post.likeCounter.innerText = post.actions_summary[0].count;
  291.  
  292.             post.actionZone.appendChild(post.replyButton);
  293.             post.actionZone.appendChild(post.likeButton);
  294.             post.actionZone.appendChild(post.likeCounter);
  295.  
  296.  
  297.         post.element.appendChild(post.headerElement);
  298.         post.element.appendChild(post.bodyElement);
  299.         post.element.appendChild(post.actionZone);
  300.         post.element.appendChild(post.repliesElement);
  301.  
  302.     }
  303.  
  304.     function toggleCollapse(event){
  305.         var thread = event.target.closest(".threaded-post");
  306.  
  307.         if ( thread.classList.contains("collapsed") ){
  308.             thread.classList.remove("collapsed");
  309.         } else {
  310.             thread.classList.add("collapsed");
  311.         }
  312.     }
  313.  
  314.  
  315.  
  316.  
  317.     Array.prototype.remove = function() {
  318.     var what, a = arguments, L = a.length, ax;
  319.     while (L && this.length) {
  320.         what = a[--L];
  321.         while ((ax = this.indexOf(what)) !== -1) {
  322.             this.splice(ax, 1);
  323.         }
  324.     }
  325.     return this;
  326. };
  327.  
  328. async function postData(url = '', data = {}) {
  329.   // Default options are marked with *
  330.   const response = await fetch(url, {
  331.     method: 'POST', // *GET, POST, PUT, DELETE, etc.
  332.     mode: 'same-origin', // no-cors, *cors, same-origin
  333.     cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
  334.     credentials: 'same-origin', // include, *same-origin, omit
  335.     headers: {
  336.       'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  337.         "sec-fetch-dest": "empty",
  338.         "sec-fetch-mode": "cors",
  339.         "sec-fetch-site": "same-origin",
  340.         "sec-gpc": "1",
  341.         "x-csrf-token": csrf,
  342.         "x-requested-with": "XMLHttpRequest",
  343.         "discourse-logged-in": "true",
  344.         "discourse-present": "true"
  345.     },
  346.     redirect: 'follow', // manual, *follow, error
  347.     referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
  348.     body: data // body data type must match "Content-Type" header
  349.   });
  350.   return response.json(); // parses JSON response into native JavaScript objects
  351. }
  352.  
  353.  
  354. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement