about summary refs log tree commit diff
path: root/app/javascript/packs
diff options
context:
space:
mode:
authorYamagishi Kazutoshi <ykzts@desire.sh>2017-06-09 22:06:38 +0900
committerEugen Rochko <eugen@zeonfederated.com>2017-06-09 15:06:38 +0200
commit140e73bc826a4c36c6f9370bc38d5d7adf9699e5 (patch)
tree823e349ad8376718f005d31d169b9b6e0571ffb5 /app/javascript/packs
parente3fae6f52c45081b69aa1f16b635cb9255983f1e (diff)
Check ready state of document in public pages (#3652)
* Check ready state of document in public pages

* add check interactive
Diffstat (limited to 'app/javascript/packs')
-rw-r--r--app/javascript/packs/public.js36
1 files changed, 21 insertions, 15 deletions
diff --git a/app/javascript/packs/public.js b/app/javascript/packs/public.js
index 0e66108bf..4c0a3d693 100644
--- a/app/javascript/packs/public.js
+++ b/app/javascript/packs/public.js
@@ -10,7 +10,7 @@ require.context('../images/', true);
 const { localeData } = getLocale();
 localeData.forEach(IntlRelativeFormat.__addLocaleData);
 
-function main() {
+function loaded() {
   const locale = document.documentElement.lang;
   const dateTimeFormat = new Intl.DateTimeFormat(locale, {
     year: 'numeric',
@@ -21,23 +21,29 @@ function main() {
   });
   const relativeFormat = new IntlRelativeFormat(locale);
 
-  document.addEventListener('DOMContentLoaded', () => {
-    [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
-      content.innerHTML = emojify(content.innerHTML);
-    });
+  [].forEach.call(document.querySelectorAll('.emojify'), (content) => {
+    content.innerHTML = emojify(content.innerHTML);
+  });
 
-    [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
-      const datetime = new Date(content.getAttribute('datetime'));
-      const formattedDate = dateTimeFormat.format(datetime);
-      content.title = formattedDate;
-      content.textContent = formattedDate;
-    });
+  [].forEach.call(document.querySelectorAll('time.formatted'), (content) => {
+    const datetime = new Date(content.getAttribute('datetime'));
+    const formattedDate = dateTimeFormat.format(datetime);
+    content.title = formattedDate;
+    content.textContent = formattedDate;
+  });
 
-    [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
-      const datetime = new Date(content.getAttribute('datetime'));
-      content.textContent = relativeFormat.format(datetime);;
-    });
+  [].forEach.call(document.querySelectorAll('time.time-ago'), (content) => {
+    const datetime = new Date(content.getAttribute('datetime'));
+    content.textContent = relativeFormat.format(datetime);;
   });
+}
+
+function main() {
+  if (['interactive', 'complete'].includes(document.readyState)) {
+    loaded();
+  } else {
+    document.addEventListener('DOMContentLoaded', loaded);
+  }
 
   delegate(document, '.video-player video', 'click', ({ target }) => {
     if (target.paused) {