Jump to content

MediaWiki:Common.js: Difference between revisions

Cdjensen94
Cdjensen94 (talk | contribs) (Created page with "Any JavaScript here will be loaded for all users on every page load.: $(document).ready(function () { // ✅ Ensure #content has the required attribute for VisualEditor const $content = $('#content'); if ($content.length && !$content.attr('data-mw-ve-target-container')) { $content.attr('data-mw-ve-target-container', 'true'); } // ✅ Ensure #mw-content-text exists inside the container if (!$('#mw-content-text').length) { $con...")
 
 
(45 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
mw.hook('wikipage.content').add(function () {
$(document).ready(function () {
  const el = document.getElementById('article-countdown');
    // ✅ Ensure #content has the required attribute for VisualEditor
  console.log('countdown hook fired; element is:', el);
    const $content = $('#content');
    if ($content.length && !$content.attr('data-mw-ve-target-container')) {
        $content.attr('data-mw-ve-target-container', 'true');
    }


    // ✅ Ensure #mw-content-text exists inside the container
  if (!el) return;
    if (!$('#mw-content-text').length) {
  el.textContent = '✅ Countdown JS is running…';
        $content.wrapInner('<div id="mw-content-text"></div>');
    }


    // ✅ Fix #ca-edit (VisualEditor entry point)
  const apiUrl = mw.util.wikiScript('api') +
    if (!$('#ca-edit').length) {
    '?action=query&meta=siteinfo&siprop=statistics&format=json&origin=*';
        $('.Continuum-header').append(
            '<a id="ca-edit" href="?action=edit" class="ve-edit-tab">Edit</a>'
        );
    }


    // ✅ Toggle VisualEditor on click
  fetch(apiUrl)
     $(document).on('click', '#ca-edit', function (e) {
     .then(r => r.json())
        e.preventDefault();
    .then(d => {
        const veUrl = window.location.href + '?veaction=edit';
      const current = d.query.statistics.articles;
        window.location.href = veUrl;
      const goal = 1000;
      const left = goal - current;
 
      el.innerHTML = left > 0
        ? `🚧 Only <strong>${left}</strong> articles to go until 1000!`
        : `🎯 We've reached 1000 articles! Let's Go Boyz and Girlz!`;
    })
    .catch(err => {
      el.textContent = 'API request failed (see console).';
      console.error(err);
     });
     });
});


    // ✅ Optional: Highlight VE when active
    if (window.location.search.includes('veaction=edit')) {
        $('#ca-edit').addClass('active');
    }
});
$(document).ready(function () {
    $('#ca-edit').after(
        '<a id="ca-source-edit" href="?action=edit" class="source-edit-tab">Source</a>'
    );


    $(document).on('click', '#ca-source-edit', function (e) {
// MediaWiki:Common.js
        e.preventDefault();
mw.hook('rcfilters.ui.initialized').add(function () {
        window.location.href = window.location.pathname + '?action=edit';
  document.documentElement.classList.add('rcfilters-ready');
    });
});
});