Score:-3

How to track recently viewed nodes using JavaScript on a completely cached website?

in flag
NSp

I want to make a block of recently viewed nodes on a website which is completely cached using Boost module (it saves a static HTML page for every request on the website). I don't want to disable Boost and recache a page every time this block updated, instead, I wondering if it possible to use JavaScript here. Is there any way I can do this?

Kevin avatar
in flag
Short answer, sure? You'd have to wire up a menu route and JS with ajax that triggers the node statistics API (I assume this is what you are talking about).
NSp avatar
in flag
NSp
@Kevin I was thinking of a client-only approach using some tracking of visited pages that are filtered by page address (if JS have such mechanic). But I'll be happy with any method available, since it's barely worth writing it from scratch.
Kevin avatar
in flag
That data still has to be stored somewhere though - right? Or are you saying just using cookie or HTML5 LocalStorage? That would all be JS at that point basically, not much Drupal. The only Drupal part might be getting that output on the page, which you could maybe do with JS dom writing anyway.
Score:0
in flag
NSp

Not a module, but still better than nothing. Note that some parts of the script are theme-aware.

This goes to the bottom of node--[type].tpl.php:

<?php if ($page): ?>
  <script type="text/javascript">updateRecentlyVisited();</script>
<?php endif; ?>

This to the new block (block's text format shouldn't change tags):

<script type="text/javascript">
var thisBlock = document.getElementById('[PUT BLOCK ID HERE]');
if (currentlyStored) {
  var recentlyVisited = '<ul">';
  for (var i = 0; i < currentlyStored.length; i++) {
    recentlyVisited += '<li><a href="' + currentlyStored[i][0] + '">' + currentlyStored[i][1] + '</a></li>';
  }
  recentlyVisited += '</ul>';

  thisBlock.innerHTML = recentlyVisited;
}
else
   thisBlock.remove();
</script>

And the script.js to attach in theme.info

var currentlyStored = JSON.parse(localStorage.getItem('recentlyVisited'));

function updateRecentlyVisited() {
    var maxLinks = 20;
    var currentPage = new Array (document.URL, document.getElementsByClassName('node-title').item(0).textContent);

    if (currentlyStored)        
        for (var i = 0; i < currentlyStored.length; i++) {
            if (currentlyStored[i][0] == currentPage[0]) {
                currentlyStored.splice([i],1);
                break;
            }
        }
    else
        currentlyStored = new Array();
    currentlyStored.unshift(currentPage);
    if (currentlyStored.length > maxLinks)
        currentlyStored.pop();
        
    localStorage.setItem('recentlyVisited', JSON.stringify(currentlyStored));
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.