Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Atelier Notifier
- // @namespace www.atelier801.com
- // @description Script que notifica por nuevos posts
- // @match http://*.atelier801.com/*
- // @include http://www.atelier801.com/*
- // @version 1.0
- // @author Eliaseeg
- // @grant none
- // ==/UserScript==
- $(document).ready(function(){
- var watchedThreads = []; // temas para parsear aquí
- var intervalTime = 1; // cantidad de segundos a chequear
- var interval;
- localThreads = JSON.parse(localStorage.getItem('threads_extension'));
- if (localThreads !== null){
- watchedThreads = localThreads;
- }
- document.addEventListener('DOMContentLoaded', function () {
- if (Notification.permission !== "granted")
- Notification.requestPermission();
- });
- // si estamos en un hilo, añadimos el botón
- if (checkUrl()){
- var currentUrl = window.location.href.toString();
- var buttonCheck = document.createElement("li");
- if (watchedThreads.length >= 1){
- var urlActual = currentUrl.substring(currentUrl.indexOf('to'), currentUrl.indexOf('&')).replace("#&", "");
- for (i = 0; i < watchedThreads.length; i++) {
- var threadSplitted = watchedThreads[i].toString().split(';');
- var topic = threadSplitted[0];
- topic = topic.substring(0, topic.indexOf('&')).replace('#&', "");
- if (topic == urlActual){
- 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>';
- break;
- }else{
- buttonCheck.innerHTML = '<a class="element-menu-contextuel" id="watch_thread"><img src="/img/icones/postit.png" class="espace-2-2 img16">¡Revisar tema!</a>';
- break;
- }
- }
- $("ul.dropdown-menu").eq(2).append(buttonCheck);
- }else{
- buttonCheck.innerHTML = '<a class="element-menu-contextuel" id="watch_thread"><img src="/img/icones/postit.png" class="espace-2-2 img16">¡Revisar tema!</a>';
- $("ul.dropdown-menu").eq(2).append(buttonCheck);
- }
- }
- // checar si estamos en la página
- $(window).on("blur focus", function(e) {
- var prevType = $(this).data("prevType");
- if (prevType != e.type) { // reduce double fire issues
- switch (e.type) {
- case "blur":
- clearInterval(interval);
- break;
- case "focus":
- interval = setInterval(function(){ checkThreads(); }, intervalTime*1000);
- break;
- }
- }
- $(this).data("prevType", e.type);
- });
- function checkThreads(){
- console.log(watchedThreads.length);
- if (watchedThreads.length >= 1){
- for (i = 0; i < watchedThreads.length; i++) {
- var threadSplitted = watchedThreads[i].toString().split(';');
- var topic = threadSplitted[0];
- var posts = threadSplitted[1];
- jQuery.ajax({
- url: topic,
- success: function(result) {
- var row = $(result).find('.corps').html();
- var postCount = $(row).length;
- $.each($(row), function(index, value) {
- if (index == postCount - 5){
- var htmlRaw = $(value).html().toString();
- htmlRaw = htmlRaw.substr(htmlRaw.search('<div id="m')-9, 100);
- htmlRaw = getPosts(htmlRaw);
- if (htmlRaw > posts){
- notifyMe(topic + "#m" + htmlRaw);
- watchedThreads[i] = topic + ';' + htmlRaw;
- localStorage.setItem("threads_extension", JSON.stringify(watchedThreads));
- }
- }
- });
- },
- });
- }
- }
- }
- // si clickean el botón, añadimos una cookie
- $("a").click(function(event) {
- if (event.target.id == "watch_thread"){
- var currentUrl = window.location.href.toString();
- var searchParams = new URLSearchParams(currentUrl.substring(currentUrl.indexOf('f'), currentUrl.length));
- var actualPage = $($('.cadre-pagination')).find('.btn').eq(2).text();
- var stripped = actualPage.substring(actualPage.indexOf('/') + '/'.length).replace(" ", "");
- jQuery.ajax({
- url: 'topic?f=' + searchParams.get('f') + '&t=' + searchParams.get('t') + '&p=' + stripped + '',
- success: function(result) {
- var row = $(result).find('.corps').html();
- var postCount = $(row).length;
- $.each($(row), function(index, value) {
- if (index == postCount - 5){
- var htmlRaw = $(value).html().toString();
- htmlRaw = htmlRaw.substr(htmlRaw.search('<div id="m')-9, 100);
- htmlRaw = getPosts(htmlRaw);
- watchedThreads.push('topic?f=' + searchParams.get('f') + '&t=' + searchParams.get('t') + '&p=' + stripped + ';'+htmlRaw);
- localStorage.setItem("threads_extension", JSON.stringify(watchedThreads));
- location.reload();
- }
- });
- },
- });
- }else if (event.target.id == "unwatch_thread"){
- var urlNow = window.location.href.toString();
- for (i = 0; i < watchedThreads.length; i++) {
- var threadSplitted = watchedThreads[i].toString().split(';');
- var topic = threadSplitted[0];
- if (urlNow.contains("#")){
- urlNow = urlNow.substring(urlNow.indexOf('to'), urlNow.indexOf('#')).replace("#m", "");
- }else{
- urlNow = urlNow.substring(urlNow.indexOf('to'), urlNow.length);
- }
- if (urlNow == topic){
- watchedThreads.splice(i, 1);
- localStorage.setItem("threads_extension", JSON.stringify(watchedThreads));
- location.reload();
- break;
- }
- }
- }
- });
- function notifyMe(urlParsed) {
- if (!Notification) {
- alert('Las notificaciones no están ºº en tu navegador. Avísame en el tema del foro qué navegador usas.');
- return;
- }
- if (Notification.permission !== "granted")
- Notification.requestPermission();
- else {
- var notification = new Notification('¡Nuevo post!', {
- icon: 'http://i.imgur.com/foovEhm.png',
- body: "¡Un tema que has revisado tiene un nuevo post! Click aquí para verlo.",
- });
- notification.onclick = function () {
- window.open("http://atelier801.com/"+urlParsed);
- };
- }
- }
- // función para checar si la página actual es un hilo
- function checkUrl(){
- var href = window.location.href.toString();
- if (href.includes("topic?f=")){
- return true;
- }
- return false;
- }
- // agarra un string y saca los números de él usando regex
- function getPosts(str) {
- var num = str.replace(/[^0-9]/g, '');
- return num;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement