Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How would I do this?

    Hello, I've been learning JavaScript recently and I've been wanting to make a script for a forum that produces a popup in the bottom right of my screen with a sound even if I'm browsing in a different tab when a new thread is posted in a specific section. I have no idea how I would even go about this.

    note: the bit about working in a different tab does not have to be the case.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others, but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


    Quizmaster: Who was the most recent king to be the subject of a Shakespeare play?
    Contestant: George VI.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    This forum is not a free coding service. As a general rule, the people helping out in this forum don't write code for others, but try to help with fixing code that doesn't work. You may perhaps get someone to write this script for you, but you'll be far more likely to get help if you have made a substantial effort and written some code yourself. Then someone here will almost certainly help you correct/improve your work.


    Quizmaster: Who was the most recent king to be the subject of a Shakespeare play?
    Contestant: George VI.
    I didn't ask anyone to write it. I don't know how I would do it.. Like how it would work in theory before I even start to write it I should know how it works.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by dannyo669 View Post
    I didn't ask anyone to write it. I don't know how I would do it.. Like how it would work in theory before I even start to write it I should know how it works.
    You are biting off far more than you can chew. In fact you will need to use server-side scripting. Javascript cannot detect if a new thread is posted in your forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    all in all, this task can be done with just a few lines of javascript...

    just use greasemonkey (or tampermonkey for chrome) to write a simple ajax script that fetches the url you want to monitor and compares the value to the last fetched value. you might consider grabbing the rss url instead of the HTML one, since sites expect the repeated pinging of feeds.

    at any rate, if the fetched responseText is different than the last fetch, send out a desktop notification using notifications
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by rnd me View Post
    all in all, this task can be done with just a few lines of javascript...
    Hmmm. I very much doubt whether the OP possesses the knowledge to take advantage of this suggestion. How about you write the "few lines" for him?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Philip M View Post
    Hmmm. I very much doubt whether the OP possesses the knowledge to take advantage of this suggestion. How about you write the "few lines" for him?
    ok, it is christmas and i had ten mins.


    i guess it was more like a dozen custom lines than a few, but i do tend to exaggerate...

    Code:
    // ==UserScript==
    // @name       CF Monitor
    // @namespace  http://danml.com/
    // @version    0.1
    // @description  monitors rss feed for thread activity, alerts user when a new thread is posted
    // @match      http://www.codingforums.com/forumdisplay.php?f=2
    // @copyright  2013, You
    // ==/UserScript==
    // made public domain by author for xmas 2013
    
    
    
    
    
    
    var rssURL="http://www.codingforums.com/external.php?type=RSS2&forumids=2"; 	      //url to monitor
    var delay=1000*60*5; // wait 5 mins between fetches
    var threads={};      // pool of known threads urls, to find new ones
    
    
    // one-time click notification setup offerer:
    if("Notification" in window && Notification.permission == "default"){ offerNotifications(); }
    
    //schedule the monitor to run automatically:
    setInterval(checkFeed, delay);
    
    //and go ahead and run it right away to memorize the current threads
    checkFeed();
    
    
    function checkFeed(){
       return  aGet(rssURL, function(doc){
            var newThreads=[];
    
        	[].slice.call(doc.getElementsByTagName("title")).map(function(title){
                   var txt=title.textContent;
                   if(!threads[txt]){
                              newThreads.push(txt);
                              threads[txt]=1;
                   }
             })
           
               notifyMe("Found new Articles", newThreads.join("\n"));
        
        });
    
    }
    
    
    
    
    
    //lib functions:
    
    function aGet(u, c) {
    	var e = new XMLHttpRequest;
    	e.onreadystatechange = function() {
    		4 == e.readyState && 200 == e.status && c(e.responseXML, e)
    	};
    	e.open("GET", u, true);
        e.setRequestHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    	e.send();
    	return e;
    }
    
    
    
    
    function offerNotifications(){
        var div=document.createElement("div");
        div.innerHTML="<button style='font-size: 60px'>Configue Desktop Notifications <br /> (click me)</button>";
        div.onclick=function(){  
            	notifyMe("Notifications Enabler", "Click the permission approval at the top of the screen.");    
        };
                               
        document.body.insertBefore(div, document.body.firstChild);
    
    }
    
    
    	
    	function notifyMe(title, description, timeOut, iconURL) {
          var tim, ops={icon:iconURL|| ("http://"+document.domain+"/favicon.ico"),  body:description||"" },
    	n="Notification" in window ? "granted" === Notification.permission ? 
    	   new Notification(title,ops) : "denied" !== Notification.permission && Notification.requestPermission(function(a) {
    		"permission" in Notification || (Notification.permission = a);
    		clearTimeout(notifyMe.tim);
    		"granted" === a && new Notification(title, ops);
    	}) : 0 || 0;
      n=n||0;
       if(!n){
       
       
    		notifyMe.tim=setTimeout(function(){
    			alert(title+"\n\n"+ (description||"")); 
    		}, 200	)
    		
    		
       }
       n.onshow=function(){  clearTimeout(notifyMe.tim); tim=setTimeout(n.close.bind(n), timeOut || 5000 );};
       n.onclick=function(){  window.focus(); n.close(); };
       return n;
    }; /* end notifyMe() */
    tested in tampermonkey and chrome devtools console, stay on main js page to activate.
    you could make it better, like jumping to the thread url if you click the notification. i'll leave the rest up to users.

    merry christmas to all and to all a good night.
    Last edited by rnd me; 12-26-2013 at 06:39 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • Users who have thanked rnd me for this post:

    dannyo669 (12-26-2013)

  • #8
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    ok, it is christmas and i had ten mins.


    i guess it was more like a dozen custom lines than a few, but i do tend to exaggerate...

    Code:
    // ==UserScript==
    // @name       CF Monitor
    // @namespace  http://danml.com/
    // @version    0.1
    // @description  monitors rss feed for thread activity, alerts user when a new thread is posted
    // @match      http://www.codingforums.com/forumdisplay.php?f=2
    // @copyright  2013, You
    // ==/UserScript==
    // made public domain by author for xmas 2013
    
    
    
    
    
    
    var rssURL="http://www.codingforums.com/external.php?type=RSS2&forumids=2"; 	      //url to monitor
    var delay=1000*60*5; // wait 5 mins between fetches
    var threads={};      // pool of known threads urls, to find new ones
    
    
    // one-time click notification setup offerer:
    if("Notification" in window && Notification.permission == "default"){ offerNotifications(); }
    
    //schedule the monitor to run automatically:
    setInterval(checkFeed, delay);
    
    //and go ahead and run it right away to memorize the current threads
    checkFeed();
    
    
    function checkFeed(){
       return  aGet(rssURL, function(doc){
            var newThreads=[];
    
        	[].slice.call(doc.getElementsByTagName("title")).map(function(title){
                   var txt=title.textContent;
                   if(!threads[txt]){
                              newThreads.push(txt);
                              threads[txt]=1;
                   }
             })
           
               notifyMe("Found new Articles", newThreads.join("\n"));
        
        });
    
    }
    
    
    
    
    
    //lib functions:
    
    function aGet(u, c) {
    	var e = new XMLHttpRequest;
    	e.onreadystatechange = function() {
    		4 == e.readyState && 200 == e.status && c(e.responseXML, e)
    	};
    	e.open("GET", u, true);
        e.setRequestHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
    	e.send();
    	return e;
    }
    
    
    
    
    function offerNotifications(){
        var div=document.createElement("div");
        div.innerHTML="<button style='font-size: 60px'>Configue Desktop Notifications <br /> (click me)</button>";
        div.onclick=function(){  
            	notifyMe("Notifications Enabler", "Click the permission approval at the top of the screen.");    
        };
                               
        document.body.insertBefore(div, document.body.firstChild);
    
    }
    
    
    	
    	function notifyMe(title, description, timeOut, iconURL) {
          var tim, ops={icon:iconURL|| ("http://"+document.domain+"/favicon.ico"),  body:description||"" },
    	n="Notification" in window ? "granted" === Notification.permission ? 
    	   new Notification(title,ops) : "denied" !== Notification.permission && Notification.requestPermission(function(a) {
    		"permission" in Notification || (Notification.permission = a);
    		clearTimeout(notifyMe.tim);
    		"granted" === a && new Notification(title, ops);
    	}) : 0 || 0;
      n=n||0;
       if(!n){
       
       
    		notifyMe.tim=setTimeout(function(){
    			alert(title+"\n\n"+ (description||"")); 
    		}, 200	)
    		
    		
       }
       n.onshow=function(){  clearTimeout(notifyMe.tim); tim=setTimeout(n.close.bind(n), timeOut || 5000 );};
       n.onclick=function(){  window.focus(); n.close(); };
       return n;
    }; /* end notifyMe() */
    tested in tampermonkey and chrome devtools console, stay on main js page to activate.
    you could make it better, like jumping to the thread url if you click the notification. i'll leave the rest up to users.

    merry christmas to all and to all a good night.
    Thanks a bunch. I just want to clear up. I never wanted anyone to do the code, but at least you were super helpful towards anyone else who posted here.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •