Advertisement
Foxscotch

BLUFF

Jan 21st, 2016
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         BLUFF
  3. // @namespace    http://foxscotch.us/
  4. // @version      0.2.2
  5. // @description  Some things to help you make userscripts for the Blockland Forum
  6. // @author       Foxscotch
  7. // @match        http://forum.blockland.us/index.php*
  8. // @grant        none
  9. // ==/UserScript==
  10. /* jshint -W097 */
  11. 'use strict';
  12.  
  13. console.log(`${GM_info.script.name} ${GM_info.script.version} running!`);
  14. var namespace = {};
  15.  
  16.  
  17. // Separates the query string  
  18. function getQueryParams(queryString) {
  19.     if (queryString[0] == '?') {
  20.         var pairs = queryString.slice(1).split(/[;&]/);
  21.     }
  22.     else {
  23.         var pairs = queryString.split(/[;&]/);
  24.     }
  25.    
  26.     var results = {};
  27.    
  28.     for (var i = 0; i < pairs.length; i++) {
  29.       var cur = pairs[i].split('=');
  30.       results[cur[0]] = cur[1];
  31.     }
  32.    
  33.     return results;
  34. }
  35. namespace.queryParams = getQueryParams(window.location.search);
  36.  
  37.  
  38. // Provides simple information about what kind of page the user is on.
  39. function getPageType() {
  40.     var type = {};
  41.     var qp = namespace.queryParams;
  42.    
  43.     if (qp.topic) {
  44.         var statusImages = document.querySelectorAll('.titlebg > td > img');
  45.         type.topic = {};
  46.        
  47.         if (statusImages[statusImages.length - 1].src.includes('locked')) {
  48.             type.topic.locked = true;
  49.         }
  50.         if (statusImages.length > 1) {
  51.             type.topic.poll = true;
  52.             if (statusImages[0].src.includes('locked')) {
  53.                 type.topic.pollLocked = true;
  54.             }
  55.         }
  56.     }
  57.     else if (qp.action == 'post' || qp.action == 'post2') {
  58.         type.post = true;
  59.     }
  60.     else if (qp.board) {
  61.         type.board = true;
  62.     }
  63.     else if (qp.action == 'profile') {
  64.         type.profile = {};
  65.        
  66.         if (!qp.sa || qp.sa == 'summary') {
  67.             type.profile.summary = true;
  68.         }
  69.         else if (qp.sa == 'statsPanel') {
  70.             type.profile.stats = true;
  71.         }
  72.         else if (qp.sa == 'showPosts') {
  73.             type.profile.posts = true;
  74.         }
  75.         else if (qp.sa == 'account') {
  76.             type.profile.accountSettings = true;
  77.         }
  78.         else if (qp.sa == 'forumProfile') {
  79.             type.profile.profileSettings = true;
  80.         }
  81.         else if (qp.sa == 'theme') {
  82.             type.profile.layoutSettings = true;
  83.         }
  84.         else if (qp.sa == 'notification') {
  85.             type.profile.notificationSettings = true;
  86.         }
  87.         else if (qp.sa == 'pmprefs') {
  88.             type.profile.pmSettings = true;
  89.         }
  90.     }
  91.     else if (qp.action == 'search') {
  92.         type.search = true;
  93.     }
  94.     else if (!window.location.search) {
  95.         type.index = true;
  96.     }
  97.     else {
  98.         type.other = true;
  99.     }
  100.  
  101.     return type;
  102. }
  103. namespace.pageType = getPageType();
  104.  
  105.  
  106. // This function gives you the current user's session ID. It gets it from the
  107. // logout button since that's guaranteed to be on every page.
  108. function getSession() {
  109.     var logout = document.querySelector('img[alt="Logout"]').parentElement;
  110.     return logout.href.split('=')[2];
  111. }
  112. namespace.sessionId = getSession();
  113.  
  114.  
  115. function User(usernameElement, avatarElement, memberId) {
  116.     this.usernameElement = usernameElement;
  117.     this.username = usernameElement.textContent.trim();
  118.    
  119.     this.avatarElement = avatarElement;
  120.    
  121.     this.memberId = memberId;
  122. }
  123.  
  124. User.prototype.updateInfo = function (doc) {
  125.     if (doc.querySelector('a[title*="Personal Message"]').children[0].alt == 'Online') {
  126.         this.online = true;
  127.     }
  128.     else {
  129.         this.online = false;
  130.     }
  131.    
  132.     this.profileElement = doc.querySelector('table.bordercolor[align=center]')
  133.    
  134.     var infoTables = this.profileElement.querySelectorAll('tr');
  135.    
  136.     this.personalText = infoTables[2].children[0].textContent.trim();
  137.    
  138.     this.blId = Number(infoTables[4].children[1].textContent);
  139.    
  140.     var postStatsRegex = /(\d+) \((\d+\.\d+) per day\)/;
  141.     var postStats = infoTables[5].children[1].textContent;
  142.     postStats = postStats.match(postStatsRegex);
  143.     this.postCount = Number(postStats[1]);
  144.     this.postsPerDay = Number(postStats[2]);
  145.    
  146.     this.gender = infoTables[17].children[1].textContent;
  147.    
  148.     var tempAge = Number(infoTables[18].children[1].textContent);
  149.     if (isNaN(tempAge)) {
  150.         this.age = 'N/A';
  151.     }
  152.     else {
  153.         this.age = tempAge;
  154.     }
  155.    
  156.     this.location = infoTables[19].children[1].textContent;
  157.    
  158.     this.sigElement = doc.querySelector('div.signature');
  159. }
  160.  
  161. User.prototype.getProfileInfo = function (funcToCall) {
  162.     var user = this;
  163.    
  164.     var request = new XMLHttpRequest();
  165.     request.responseType = 'document';
  166.    
  167.     request.onload = function () {
  168.         user.updateInfo(this.response);
  169.         if (funcToCall) {
  170.             funcToCall(user);
  171.         }
  172.     };
  173.    
  174.     request.open('get', 'http://forum.blockland.us/index.php?action=profile;u=' + user.memberId);
  175.     request.send();
  176. }
  177.  
  178. namespace.User = User;
  179.  
  180.  
  181. if (namespace.pageType.profile && namespace.pageType.profile.summary) {
  182.     var name = document.querySelectorAll('tr.titlebg > td')[0];
  183.     var av = document.querySelector('.avatar');
  184.    
  185.     var params = getQueryParams(document.querySelectorAll('td.windowbg2[colspan="2"] > a')[0].href.split('?')[1]);
  186.     var id = params.u;
  187.    
  188.     namespace.currentUser = new User(name, av, id);
  189.     namespace.currentUser.updateInfo(document);
  190. }
  191.  
  192.  
  193. // This is not my doing, but for your information, window.ajax_indicator() is
  194. // good for indicating that you are loading.
  195.  
  196. // window.ajax_indicator(true); turns it on, and...
  197. // window.ajax_indicator(false); turns it off!
  198.  
  199. // I intend to add better facilities for this kind of thing later, because using
  200. // this method, if someone else is loading something, yours will turn theirs off
  201. // whenever it finishes.
  202.  
  203.  
  204. window.bluff = namespace;
  205.  
  206. var finishedEvent = new Event('utilsFinished');
  207. document.dispatchEvent(finishedEvent);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement