agent-for-hackmd.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * GROWI agent for HackMD
  3. *
  4. * This file will be transpiled as a single JS
  5. * and should be load from HackMD head via 'lib/routes/hackmd.js' route
  6. *
  7. * USAGE:
  8. * <script src="${hostname of GROWI}/_hackmd/load-agent"></script>
  9. *
  10. * @author Yuki Takei <yuki@weseek.co.jp>
  11. */
  12. /* eslint-disable no-console */
  13. console.log('Loading GROWI agent for HackMD...');
  14. const allowedOrigin = '{{origin}}'; // will be replaced by swig
  15. const styleFilePath = '{{styleFilePath}}'; // will be replaced by swig
  16. /**
  17. * Validate origin
  18. * @param {object} event
  19. */
  20. function validateOrigin(event) {
  21. if (event.origin !== allowedOrigin) {
  22. console.error('Rejected', 'Cause: "event.origin" and "allowedOrigin" does not match');
  23. return;
  24. }
  25. }
  26. function insertStyle() {
  27. const element = document.createElement('link');
  28. element.href = styleFilePath;
  29. element.rel = 'stylesheet';
  30. document.getElementsByTagName('head')[0].appendChild(element);
  31. }
  32. insertStyle();
  33. window.addEventListener('message', (event) => {
  34. validateOrigin(event);
  35. console.log('getValue called');
  36. });
  37. window.addEventListener('load', (event) => {
  38. console.log('loaded');
  39. });
  40. console.log('GROWI agent for HackMD has successfully loaded.');