סקריפט JavaScript מותאם אישית מופעל רק לאחר רענון, יצירת קטגוריות עבור תוסף מיקום

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>

אם אינני טועה, מכיוון ש-Discourse היא אפליקציית דף יחיד, אני מאמין שתצטרך להשתמש ב-api.onPageChange() של ה-plugin API עבור זה.

4 לייקים

נכון, ושימוש בסקריפטים עומד להיות מוצא משימוש.

2 לייקים

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

בסדר, אם JavaScript בכותרות יופסק בקרוב, אצטרך להתאים כמה דברים, אולי שדה הכותרת הזה יכול לקבל הודעה לגבי המצב המתקרב.

אני מניח שיש לו סיבות, אבל זה יכול להיות חבל מכיוון שסקריפטים בכותרות שימושיים ומאפשרים התאמות מהירות למנהלים כמו: סגירת רישום אירועים כאשר מספר ספציפי תואם על ידי פונקציית JavaScript עבור קטגוריה ספציפית.

אני מצטער שעניתי לך במקום לג’יי שאומר שסקריפטים הולכים להיות מיושנים, אני מדמיין שהוא מדבר על סקריפטים בכותרת שפשוט מאוד נוחים לשימוש לשינויים מהירים בדיוק כמו בכל מערכת ניהול תוכן של אתר אינטרנט.

מצאתי למעשה פתרון שמטעין מחדש רק את דף ה"מפה" בכל פעם שאתה נכנס אליו.

אני תוהה כעת אם כדאי לשתף את זה כאן אם סקריפטים בכותרת יופסקו בעתיד? הסקריפט בפוסט הזה מאפשר קטגוריות צבע לפי התוכן של כותרת אירוע במפה, זה אמור להיות ניתן לשינוי במהירות ולא דרך תכונת ה-API.

`

`

לייק 1

אני חושב שהבנת אותי לא נכון. התכוונתי שתגיות script בכותרת כמו \u003cscript type=\"text/discourse-plugin\"\u003e ו-\u003cscript type=\"text/x-handlebars\"\u003e הן מיושנות, לא תגיות \u003cscript\u003e רגילות.

2 לייקים

כן, תודה על ההבהרה, אני שמח שאנחנו יכולים להשתמש בג’אווהסקריפט, בדוק את הקוד שהשארתי אם יש לך זמן, הוא עדיין עשוי להיות שימושי בסביבות ember, הוא טוען מחדש את הדף כך שה-js יופעל. זה לא הכי נחמד אבל זה עובד טוב עבור דף תוסף המפה!

2 לייקים

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