Suppenbiatch

BetterRedditShare

Feb 7th, 2022
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         BetterRedditShare
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1
  5. // @description  try to take over the world!
  6. // @author       Suppe
  7. // @match        https://old.reddit.com/*
  8. // @icon         https://www.redditstatic.com/desktop2x/img/favicon/favicon-32x32.png
  9. // @grant        GM.setValue
  10. // @grant        GM.getValue
  11. // @grant        GM_xmlhttpRequest
  12. // @connect      10.0.0.20
  13. // @connect      10.0.0.21
  14. // @connect      public ip here
  15. // ==/UserScript==
  16.  
  17. (async () => {
  18.     'use strict';
  19.  
  20.     var NO_USER_ID = false;
  21.     var user_id_awaited = await GM.getValue("user_id");
  22.     if(user_id_awaited === undefined || user_id_awaited === null || user_id_awaited === ""){
  23.         await GM.setValue("user_id", prompt("Enter your Discord User ID."));
  24.     }
  25.  
  26.     /*
  27.     var user_id_awaited = await GM.getValue("user_id");
  28.     if(user_id_awaited === undefined || user_id_awaited === null || user_id_awaited === ""){
  29.         NO_USER_ID = true; // Resets after page reload, still allows local title to be replaced
  30.         console.log("NO USER ID PRESENT");
  31.     }
  32.     */
  33.     const USER_ID = await GM.getValue("user_id");
  34.  
  35.     var lastChangedItem = 0;
  36.  
  37.     function shareItem (e, url) {
  38.         var target = e.target || e.srcElement;
  39.        
  40.         console.log(`shareing: "${url}" with user id: "${USER_ID}"`);
  41.         const data = {'user': USER_ID, 'url': url}
  42.         const discord_bot_url = "http://10.0.0.21:8084/share";
  43.         target.style.color = 'DeepPink';
  44.  
  45.         GM_xmlhttpRequest ( {
  46.             method: "POST",
  47.             url: discord_bot_url,
  48.             data: JSON.stringify(data),
  49.             onload: function (r) {
  50.                 switch (r.status) {
  51.                     case 200:
  52.                         target.style.color = 'green';
  53.                         break;
  54.                     case 403:
  55.                         console.log(r.responseText);
  56.                         target.style.color = 'blue';
  57.                         break;
  58.                     default:
  59.                         console.log(r.responseText);
  60.                         target.style.color = 'red';
  61.                 }
  62.             }
  63.         })
  64.  
  65.         e.preventDefault();
  66.     }
  67.  
  68.     function overwriteShare(ele, idx, arr) {
  69.         if (lastChangedItem > idx) return;
  70.  
  71.         try {
  72.             var PostCommentContainer = ele.querySelector('a[data-event-action="comments"]');
  73.             var post_url = PostCommentContainer.href;
  74.         }
  75.         catch (e) {
  76.             return;
  77.         }
  78.  
  79.         var shareHeader = ele.querySelector('.share')
  80.         shareHeader.innerHTML = ''
  81.  
  82.         //<a id="myLink" href="#" onclick="MyFunction();return false;">link text</a>
  83.         var shareButton = document.createElement('a');
  84.         shareButton.id = 'DiscordShare'
  85.         shareButton.href = '#'
  86.         shareButton.onclick = (e) => shareItem(e, post_url)
  87.         shareButton.innerText = 'share'
  88.  
  89.         shareHeader.appendChild(shareButton);
  90.  
  91.         lastChangedItem += 1;
  92.  
  93.     }
  94.  
  95.     function getAllPosts() {
  96.         if (NO_USER_ID) {
  97.             return;
  98.         }
  99.         var nodes = document.querySelectorAll('div[class^="entry"]')
  100.         nodes.forEach(overwriteShare)
  101.     }
  102.  
  103.     setInterval(getAllPosts, 1000);
  104.  
  105.     // Your code here...
  106. })();
Add Comment
Please, Sign In to add comment