(function() {
    console.log('Canonical URL Script is running');  // Add this to confirm the script is running

    // Get the base instance URL
    var instanceURL = window.location.origin;

    // Get the sysparm_article from the URL
    var urlParams = new URLSearchParams(window.location.search);
    var articleNumber = urlParams.get('sysparm_article');

    if (articleNumber) {
        // Create the canonical link tag
        var link = document.createElement('link');
        link.rel = 'canonical';
        link.href = instanceURL + '/kb?id=kb_article_view&sysparm_article=' + articleNumber;

        // Append the canonical link to the head of the document
        document.head.appendChild(link);
        console.log('Canonical URL added to the head:', link.href); // Log the canonical URL
    } else {
        console.log('No article number found in the URL.');
    }
})();
