Advertisement
Eliaseeg

AtelierNotifier

Jan 18th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name        Atelier Notifier
  3. // @namespace   www.atelier801.com
  4. // @description Script que notifica por nuevos posts
  5. // @match       http://*.atelier801.com/*
  6. // @include     http://www.atelier801.com/*
  7. // @version     1.0
  8. // @author      Eliaseeg
  9. // @grant       none
  10.  
  11. // ==/UserScript==
  12. $(document).ready(function(){
  13.  
  14.     var watchedThreads = []; // temas para parsear aquí
  15.     var intervalTime = 1; // cantidad de segundos a chequear
  16.     var interval;
  17.  
  18.     localThreads = JSON.parse(localStorage.getItem('threads_extension'));
  19.     if (localThreads !== null){
  20.         watchedThreads = localThreads;
  21.     }
  22.  
  23.     document.addEventListener('DOMContentLoaded', function () {
  24.       if (Notification.permission !== "granted")
  25.         Notification.requestPermission();
  26.     });
  27.  
  28.     // si estamos en un hilo, añadimos el botón
  29.     if (checkUrl()){
  30.         var currentUrl = window.location.href.toString();
  31.         var buttonCheck = document.createElement("li");
  32.         if (watchedThreads.length >= 1){
  33.             var urlActual = currentUrl.substring(currentUrl.indexOf('to'), currentUrl.indexOf('&')).replace("#&", "");
  34.             for (i = 0; i < watchedThreads.length; i++) {
  35.                 var threadSplitted = watchedThreads[i].toString().split(';');
  36.                 var topic = threadSplitted[0];
  37.                 topic = topic.substring(0, topic.indexOf('&')).replace('#&', "");
  38.                 if (topic == urlActual){
  39.                     buttonCheck.innerHTML = '<a class="element-menu-contextuel" id="unwatch_thread"><img src="/img/icones/cadenas.png" class="espace-2-2 img16">¡Dejar de revisar tema!</a>';
  40.                     break;
  41.                 }else{
  42.                     buttonCheck.innerHTML = '<a class="element-menu-contextuel" id="watch_thread"><img src="/img/icones/postit.png" class="espace-2-2 img16">¡Revisar tema!</a>';
  43.                     break;
  44.                 }
  45.             }
  46.             $("ul.dropdown-menu").eq(2).append(buttonCheck);
  47.         }else{
  48.             buttonCheck.innerHTML = '<a class="element-menu-contextuel" id="watch_thread"><img src="/img/icones/postit.png" class="espace-2-2 img16">¡Revisar tema!</a>';
  49.             $("ul.dropdown-menu").eq(2).append(buttonCheck);
  50.         }
  51.     }
  52.  
  53.     // checar si estamos en la página
  54.     $(window).on("blur focus", function(e) {
  55.         var prevType = $(this).data("prevType");
  56.         if (prevType != e.type) {   //  reduce double fire issues
  57.             switch (e.type) {
  58.                 case "blur":
  59.                     clearInterval(interval);
  60.                     break;
  61.                 case "focus":
  62.                     interval = setInterval(function(){ checkThreads(); }, intervalTime*1000);
  63.                     break;
  64.             }
  65.         }
  66.         $(this).data("prevType", e.type);
  67.     });
  68.  
  69.     function checkThreads(){
  70.         console.log(watchedThreads.length);
  71.         if (watchedThreads.length >= 1){
  72.             for (i = 0; i < watchedThreads.length; i++) {
  73.                 var threadSplitted = watchedThreads[i].toString().split(';');
  74.                 var topic = threadSplitted[0];
  75.                 var posts = threadSplitted[1];
  76.                 jQuery.ajax({
  77.                     url: topic,
  78.                     success: function(result) {
  79.                         var row = $(result).find('.corps').html();
  80.                         var postCount = $(row).length;
  81.                         $.each($(row), function(index, value) {
  82.                             if (index == postCount - 5){
  83.                                 var htmlRaw = $(value).html().toString();
  84.                                 htmlRaw = htmlRaw.substr(htmlRaw.search('<div id="m')-9, 100);
  85.                                 htmlRaw = getPosts(htmlRaw);
  86.                                 if (htmlRaw > posts){
  87.                                     notifyMe(topic + "#m" + htmlRaw);
  88.                                     watchedThreads[i] = topic + ';' + htmlRaw;
  89.                                     localStorage.setItem("threads_extension", JSON.stringify(watchedThreads));
  90.                                 }
  91.                             }
  92.                         });
  93.                     },
  94.                 });
  95.             }
  96.         }
  97.     }
  98.  
  99.     // si clickean el botón, añadimos una cookie
  100.     $("a").click(function(event) {
  101.         if (event.target.id == "watch_thread"){
  102.             var currentUrl = window.location.href.toString();
  103.             var searchParams = new URLSearchParams(currentUrl.substring(currentUrl.indexOf('f'), currentUrl.length));
  104.             var actualPage = $($('.cadre-pagination')).find('.btn').eq(2).text();
  105.             var stripped = actualPage.substring(actualPage.indexOf('/') + '/'.length).replace(" ", "");
  106.             jQuery.ajax({
  107.                 url: 'topic?f=' + searchParams.get('f') + '&t=' + searchParams.get('t') +  '&p=' + stripped + '',
  108.                 success: function(result) {
  109.                     var row = $(result).find('.corps').html();
  110.                     var postCount = $(row).length;
  111.                     $.each($(row), function(index, value) {
  112.                         if (index == postCount - 5){
  113.                             var htmlRaw = $(value).html().toString();
  114.                             htmlRaw = htmlRaw.substr(htmlRaw.search('<div id="m')-9, 100);
  115.                             htmlRaw = getPosts(htmlRaw);
  116.                             watchedThreads.push('topic?f=' + searchParams.get('f') + '&t=' + searchParams.get('t') +  '&p=' + stripped + ';'+htmlRaw);
  117.                             localStorage.setItem("threads_extension", JSON.stringify(watchedThreads));
  118.                             location.reload();
  119.                         }
  120.                     });
  121.                 },
  122.             });
  123.         }else if (event.target.id == "unwatch_thread"){
  124.             var urlNow = window.location.href.toString();
  125.             for (i = 0; i < watchedThreads.length; i++) {
  126.                 var threadSplitted = watchedThreads[i].toString().split(';');
  127.                 var topic = threadSplitted[0];     
  128.                 if (urlNow.contains("#")){
  129.                     urlNow = urlNow.substring(urlNow.indexOf('to'), urlNow.indexOf('#')).replace("#m", "");
  130.                 }else{
  131.                     urlNow = urlNow.substring(urlNow.indexOf('to'), urlNow.length);
  132.                 }
  133.                 if (urlNow == topic){
  134.                     watchedThreads.splice(i, 1);
  135.                     localStorage.setItem("threads_extension", JSON.stringify(watchedThreads));
  136.                     location.reload();
  137.                     break;
  138.                 }
  139.             }
  140.         }
  141.     });
  142.  
  143.     function notifyMe(urlParsed) {
  144.         if (!Notification) {
  145.             alert('Las notificaciones no están     ºº en tu navegador. Avísame en el tema del foro qué navegador usas.');
  146.             return;
  147.         }
  148.         if (Notification.permission !== "granted")
  149.             Notification.requestPermission();
  150.         else {
  151.             var notification = new Notification('¡Nuevo post!', {
  152.                 icon: 'http://i.imgur.com/foovEhm.png',
  153.                 body: "¡Un tema que has revisado tiene un nuevo post! Click aquí para verlo.",
  154.             });
  155.             notification.onclick = function () {
  156.                 window.open("http://atelier801.com/"+urlParsed);
  157.             };
  158.         }
  159.     }
  160.  
  161.     // función para checar si la página actual es un hilo
  162.     function checkUrl(){
  163.         var href = window.location.href.toString();
  164.         if (href.includes("topic?f=")){
  165.             return true;
  166.         }
  167.         return false;
  168.     }
  169.  
  170.     // agarra un string y saca los números de él usando regex
  171.     function getPosts(str) {
  172.         var num = str.replace(/[^0-9]/g, '');
  173.         return num;
  174.     }
  175.  
  176. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement