Aangepaste javascript wordt pas toegepast na vernieuwing, categorieën maken voor locatieplugin

Hi,

The below script shows in the location plugin the tooltip permanently and give them colors to sort them according to their type(events, venues, etc), I’m working on a legend and maybe a filter.

The situation is that It does work, but only after I refresh the page, it does not work when landing on the page from the url.

There is no differences in the errors in the console in both cases and adding a higher delay to the function does not change a thing.

Has anyone ever experienced this when adding js to the header from the edit theme menu and found a solution?

Here is a picture of the result

the errors here appear with or without the code so they are not related to it

<script>
document.addEventListener('DOMContentLoaded', function() {
  // Apply CSS to change the alt text style
  var style = document.createElement('style');
  style.type = 'text/css';
  style.innerHTML = `
    /* Ensure alt text is bold, underlined, and has conditional color */
    .leaflet-marker-pane img[alt] {
      font-weight: bold !important; /* Make text bold */
      text-decoration: underline !important; /* Underline the text */
      white-space: nowrap; /* Prevent the text from wrapping to a new line */
    }
  `;
  document.head.appendChild(style);

  // Give the page a moment to load and the images to be rendered
  setTimeout(function() {
    var images = document.querySelectorAll('.leaflet-marker-pane img');

    images.forEach(function(img) {
      var title = img.getAttribute('title');
      if (title) {
        title = title.length > 10 ? title.substring(0, 10) + '..' : title;
        img.setAttribute('alt', title);
        img.setAttribute('title', title);

        if (title.includes('/')) {
          img.style.color = 'white';
        } else {
          img.style.color = 'black';
        }
      }
      img.setAttribute('src', '');
    });
  }, 1000);  // Delay execution by 1 second (1000 ms)
});



</script>

Als ik me niet vergis, omdat Discourse een single-page applicatie is, denk ik dat je api.onPageChange() van de plugin API moet gebruiken hiervoor.

4 likes

Oké, en het gebruik van scripts wordt binnenkort als verouderd beschouwd.

2 likes

Good to know, I won’t bother using an adapted window.location.reload(true) in the header for an ember.js framework which might have been a solution for this.

Is the header field being deprecated out of security reasons? The thing is that it is still convenient to test vanilla javascript in it.

No, using the plugin api in script tags (in the header) is deprecated.

1 like

ok als javascript in headers binnenkort verouderd is, moet ik een paar dingen aanpassen, misschien kan dit header-veld een melding bevatten over deze aankomende situatie.

Ik denk dat het zijn redenen heeft, maar het kan jammer zijn, aangezien scripts in headers nuttig zijn en snelle aanpassingen voor beheerders mogelijk maken, zoals: het sluiten van evenementregistratie wanneer een specifiek aantal wordt bereikt door een javascript-functie voor een specifieke categorie.

sorry I answered to you instead of Jay who says that scripts are going to be deprecated, I imagine he is speaking about header scripts that are just very convenient to use for quick modifications just like in any website CMS.

Ik heb eigenlijk een oplossing gevonden die alleen de “map”-pagina opnieuw laadt telkens wanneer je erop gaat.

Ik vraag me nu af of dit de moeite waard is om hier met anderen te delen als header-scripts verouderd zullen zijn? Het script in dit bericht maakt kleurencategorieën mogelijk volgens de inhoud van een evenementtitel op de kaart, dit zou snel veranderbaar moeten zijn, niet via de API-functie.

<script> (function(){function e(){if(location.pathname.includes("/map")){if(!sessionStorage.getItem("map-refreshed")){sessionStorage.setItem("map-refreshed","true");requestAnimationFrame(()=>setTimeout(()=>location.reload(!0),100))}}}function t(){sessionStorage.removeItem("map-refreshed")}function n(e){const t=history[e];history[e]=function(){const n=t.apply(this,arguments);return window.dispatchEvent(new Event("locationchange")),n}}n("pushState"),n("replaceState"),addEventListener("popstate",()=>dispatchEvent(new Event("locationchange"))),addEventListener("locationchange",function(){location.pathname.includes("/map")?e():t()}),e();})(); </script>

1 like

Ik denk dat u mij verkeerd begrepen heeft. Ik bedoelde dat script-tags in de header zoals \u003cscript type=\"text/discourse-plugin\"\u003e en \u003cscript type=\"text/x-handlebars\"\u003e verouderd zijn, niet normale \u003cscript\u003e-tags.

2 likes

ja, bedankt voor de verduidelijking, ik ben blij dat we javascript kunnen plaatsen, bekijk de code die ik heb achtergelaten als je tijd hebt, deze kan nog steeds nuttig zijn in ember-omgevingen, het laadt de pagina opnieuw, zodat de js wordt toegepast. Het is niet het mooiste, maar het werkt goed voor de kaartpluginpagina!

2 likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.