www.webdeveloper.com
Results 1 to 2 of 2

Thread: Greasemonkey count clicks

  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Greasemonkey count clicks

    Hi! I've just started some JavaScript development, but have run into a bit of a problem with a Greasemonkey script I'm working on. I've searched and searched, but have run into far too much click-counting spam to really figure this out. Any help is much appreciated.

    Summary: This script is for personal use. I'm doing a variety of things on a page, including auto-refreshing it. I want to display the number of clicks (preferably just on links) since the last refresh. I know posts that don't have any code are generally looked down upon, but nothing I've tried has worked and I'd rather not confuse people with my noobish code.

    Once again, thanks for any help.

  2. #2
    Join Date
    Jan 2010
    Posts
    2
    Alright, let me update with some code in hopes of getting a response here.

    Below is the code in its current condition. Remember, I'm knew at this, so please forgive/correct any newb mistakes.

    Code:
    window.setInterval(settime, 1000);
    
    window.setInterval(refreshpage, 300000);
    
    function settime () {
       var curTime = new Date();
       var curHour = curTime.getHours();
       var curMin = curTime.getMinutes();
       var curSec = curTime.getSeconds();
    
       if (curMin < 10) {
          curMin = "0" + curMin;
       }
    
       if (curSec < 10) {
          curSec = "0" + curSec;
       }
       var time = "Current time is: " + curHour + ":" + curMin + ":" + curSec;
    
       var myField = document.getElementById('Current');
       myField.innerHTML = time;
    }
    
    function refreshpage() {
       window.location.reload();
    }
    
    // Time stuff
    var time = new Date();
    var hours = time.getHours();
    var minutes = time.getMinutes();
    var seconds = time.getSeconds();
    
    if (minutes < 10) {
       minutes = "0" + minutes;
    }
    
    if (seconds < 10) {
       seconds = "0" + seconds;
    }
    
    // Text to display
    var refreshTime = "Page last refreshed: " + hours + ":" + minutes + ":" + seconds;
    var firstTime = "Current time is: " + hours + ":" + minutes + ":" + seconds;
    
    var divTime = document.createElement('div');
    divTime.setAttribute("style", "background: #ffffff; position: fixed; vertical-align:top; text-align:left!important; text-align:center; top:85px; right:0px; padding:3px;");
    divTime.innerHTML = firstTime;
    divTime.setAttribute("id", "Current");
    document.body.insertBefore(divTime, document.body.firstChild);
    
    var divClock = document.createElement('div');
    divClock.setAttribute("style", "background: #ffffff; position: fixed; vertical-align:top; text-align:left!important; text-align:center; top:70px; right:0px; padding:3px;");
    divClock.innerHTML = refreshTime;
    divClock.setAttribute("id","Clock");
    document.body.insertBefore(divClock, document.body.firstChild);
    Now, my main problem with counting clicks, I believe, is that I'm using Greasemonkey. If I had were writing on the page itself, I'd either write a simple counting function and call it onClick from each <a> element or do the same thing for the <body> element and just be content with counting total clicks on the page. If anyone could point me towards way to loop through all <a> elements and add an onClick to each or the same thing for the body, I could probably work out the rest of it.

    Thanks in advance.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles