User:Korok/articlehistory.js

From Traveller Wiki - Science-Fiction Adventure in the Far future
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
// Check if Local Storage is available
function storageAvailable(type) {
    var storage;
    try {
        storage = window[type];
        var x = '__storage_test__';
        storage.setItem(x, x);
        storage.removeItem(x);
        return true;
    }
    catch(e) {
        return e instanceof DOMException && (
            // everything except Firefox
            e.code === 22 ||
            // Firefox
            e.code === 1014 ||
            // test name field too, because code might not be present
            e.name === 'QuotaExceededError' ||
            // Firefox
            e.name === 'NS_ERROR_DOM_QUOTA_REACHED') &&
            // acknowledge QuotaExceededError only if there's something already stored
            (storage && storage.length !== 0);
    }
}

if (storageAvailable('localStorage')) {
    // Function to save the current page title to local storage
    function savePageVisit() {
        var pageTitle = mw.config.get('wgPageName');
        var visits = localStorage.getItem('visitedArticles');
        if (visits) {
            visits = JSON.parse(visits);
            if (!visits.includes(pageTitle)) {
                visits.push(pageTitle);
            }
        } else {
            visits = [pageTitle];
        }
        localStorage.setItem('visitedArticles', JSON.stringify(visits));
    }

    // Function to display visited articles
    function showVisitedArticles() {
        var visits = localStorage.getItem('visitedArticles');
        if (visits) {
            visits = JSON.parse(visits);
            var html = '<ul>';
            visits.forEach(function(page) {
                html += '<li><a href="/wiki/' + encodeURIComponent(page) + '">' + page.replace(/_/g, ' ') + '</a></li>';
            });
            html += '</ul>';
            $('#visitedArticles').html(html);
        }
    }

    // Save every visited page
    $(document).ready(function() {
        savePageVisit();
    });
} else {
    console.log("Local Storage is not available in your browser.");
}