Loading...
 
Skip to main content

History: MenuPageBanner

Source of version: 10 (current)

Copy to clipboard
            {rss url="https://tiki.org/tiki-calendars_rss.php" max="1" date="1" desc="1" author="1" showtitle="1" ticker="1" sortBy="publication_date" sortOrder="ASC" tplWiki="rss_gabarit" refresh="360" }
{JQ()}
var $scope = $('.upcoming-event-banner');

  /**
   * Extracts the calendar item ID (calitemId) from a URL string
   * Handles both encoded and non-encoded URLs
   */
  function extractCalItemId(rawUrl) {
    if (!rawUrl) return null;
    var decoded = rawUrl;
    try { decoded = decodeURIComponent(rawUrl); } catch (e) {}
    var m = decoded.match(/(?:^|[?&])calitemId=([^&#]+)/i);
    return m ? m[1] : null;
  }

  /**
   * Builds the final iCal export URL from the extracted calitemId
   */
  function buildIcalUrl(id) {
    return 'https://tiki.org/tiki-calendar_export_ical.php?export=y&calendarItem=' + encodeURIComponent(id);
  }

  // 1) Convert <a> WITHOUT target="_blank" → iCal export link
  $scope.find('a:not([target="_blank"])').each(function () {
    var $a = $(this);
    var href = $a.attr('href');
    if (!href) return;

    // Skip if it's already an iCal export link
    if (/tiki-calendar_export_ical\.php/i.test(href)) return;

    // Try to extract calitemId from href or data-href
    var id = extractCalItemId(href) || (this.dataset && this.dataset.href ? extractCalItemId(this.dataset.href) : null);
    if (id) $a.attr('href', buildIcalUrl(id));
  });

  // 2) Convert <a> WITH target="_blank" → decode encoded URLs
  $scope.find('a[target="_blank"]').each(function () {
    var $a = $(this);
    var href = $a.attr('href');
    if (!href) return;

    // Only process if the URL is encoded (contains %3F, %3D, etc.)
    if (/%3F|%3D|%26|%2F/i.test(href)) {
      try {
        var decoded = decodeURIComponent(href);
        $a.attr('href', decoded);
      } catch (e) {
        // Fail silently if decoding throws an error
      }
    }
  });
{JQ}