Cloudron makes it easy to run web apps like WordPress, Nextcloud, GitLab on your server. Find out more or install now.


Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Bookmarks
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

Cloudron Forum

Apps | Demo | Docs | Install
  1. Cloudron Forum
  2. Moodle
  3. Moodle Bug: Unable to inject <script>javascript code</script> in blocks or course activities

Moodle Bug: Unable to inject <script>javascript code</script> in blocks or course activities

Scheduled Pinned Locked Moved Solved Moodle
18 Posts 3 Posters 2.3k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    taowang
    wrote on last edited by
    #1

    I want to embed the following chatbot, but injecting the javascript does not work on my cloudron moodle instance.

    After testing on different instances, it turns out this might be a cloudron packaging bug.

    I tested the following code on 4 moodle instances:

    • moodle demo: https://sandbox.moodledemo.net/
    • moodle for mac: https://download.moodle.org/macosx/
    • my original cloudron moodle
    • new cloudron moodle

    moodle demo and moodle for mac work fine, but cloudron moodles do not work.

    Steps to recreate the issue

    1. install moodle and change editor preference to Plain Text
      254f350f-9c3e-4258-a379-2d857add05f6-image.png
    2. create a course and add an activity
    3. copy and paste the following code
    <flowise-fullchatbot></flowise-fullchatbot>
    <script type="module">
        import Chatbot from "https://unpkg.com/flowise-embed/dist/web.js"
        Chatbot.initFull({
            chatflowid: "f5adc8af-d9e3-4735-af79-886f237c354e",
            apiHost: "https://flowise.riverhill.ai",
            theme: {
                chatWindow: {
                    showTitle: false,
                    title: '',
                    titleAvatarSrc: '',
                    welcomeMessage: '',
                    errorMessage: 'Error! Please contact twang293@gmail.com',
                    backgroundColor: "#fff",
                    fontSize: 16,
                    botMessage: {
                        backgroundColor: "#fafafa",
                        textColor: "#000",
                        showAvatar: true,
                        avatarSrc: "https://images.riverhill.ai/circle-offwhite.png",
                    },
                    userMessage: {
                        backgroundColor: "#3B81F6",
                        textColor: "#ffffff",
                        showAvatar: false,
                        avatarSrc: "",
                    },
                    textInput: {
                        placeholder: '',
                        backgroundColor: '#ffffff',
                        textColor: '#303235',
                        sendButtonColor: '#3B81F6',
                        maxChars: 300,
                        maxCharsWarningMessage: 'Maximum Characters = 500',
                        autoFocus: true,
                        sendMessageSound: false,
                        receiveMessageSound: false,
                    },
                    feedback: {
                        color: '#303235',
                    },
                    footer: {
                        textColor: '#',
                        text: '',
                        company: '',
                        companyLink: '',
                    }
                }
            }
        })
    </script>
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      taowang
      wrote on last edited by
      #2

      Any updates?

      1 Reply Last reply
      0
      • nebulonN Away
        nebulonN Away
        nebulon
        Staff
        wrote on last edited by
        #3

        This might very well be a CSP issue where the browser just blocks sourcing of external scripts. Do you see any errors in your browser console which might be related?

        T 1 Reply Last reply
        0
        • nebulonN nebulon

          This might very well be a CSP issue where the browser just blocks sourcing of external scripts. Do you see any errors in your browser console which might be related?

          T Offline
          T Offline
          taowang
          wrote on last edited by
          #4

          @nebulon probably not CSP, because the script can be injected into the "additional html" section. It just cannot be injected directly in course activities or blocks.

          It works fine on other instances. I can inject the script any where.

          The problem is I already have too much code in the "additional html" settings. I want to inject the script along with its html. And I need to embed several of these chatbots in the near future. So the better practice is to isolate these snippets.

          1 Reply Last reply
          0
          • T Offline
            T Offline
            taowang
            wrote on last edited by
            #5

            By "additional html" setting, I mean "site admin > appearance > additional html"

            image.png

            1 Reply Last reply
            0
            • nebulonN Away
              nebulonN Away
              nebulon
              Staff
              wrote on last edited by nebulon
              #6

              I guess we need someone here with basic moodle experience at least then. I am not really following the issue tbh. So you cannot submit a form to inject the code or is the code just not injected or is there any error anywhere? But also injecting the same code in the "Additional HTML" feature works as expected?

              It could be that other places to inject code requires moodle to write to a read-only location on disk, but then some error should be shown to get more insight.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                taowang
                wrote on last edited by taowang
                #7

                Injecting the script here works:

                image.png

                But injecting in course page does not work:
                image.png

                In the console, there are some errors:
                feb3178b-d13e-4f6d-8d99-186275c99505-image.png

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  taowang
                  wrote on last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • T Offline
                    T Offline
                    taowang
                    wrote on last edited by
                    #9

                    But these errors are not related to the injection problem.

                    1 Reply Last reply
                    0
                    • nebulonN nebulon marked this topic as a question on
                    • nebulonN Away
                      nebulonN Away
                      nebulon
                      Staff
                      wrote on last edited by
                      #10

                      Those errors seem more like a moodle internal problem and possibly bugs worth reporting upstream unless those come from some of the custom code snippets.

                      T 1 Reply Last reply
                      0
                      • nebulonN nebulon

                        Those errors seem more like a moodle internal problem and possibly bugs worth reporting upstream unless those come from some of the custom code snippets.

                        T Offline
                        T Offline
                        taowang
                        wrote on last edited by
                        #11

                        @nebulon yes those bugs are related to my other code snippets, not related to moodle core. The chatbot code itself does not have bugs, because I test it on fresh new moodle instances.

                        1 Reply Last reply
                        0
                        • T Offline
                          T Offline
                          taowang
                          wrote on last edited by
                          #12

                          I recorded a video to demonstrate the issue.

                          https://images.riverhill.ai/王韬·江山学府 2024-08-08 18.34.48.mp4

                          1 Reply Last reply
                          0
                          • T Offline
                            T Offline
                            taowang
                            wrote on last edited by
                            #13

                            After deeper investigation, it is likely related to CSP.
                            But I don't know how the moodle demo site sets its CSP to allow script and css injection directly into course pages.

                            1 Reply Last reply
                            0
                            • J Offline
                              J Offline
                              joseph
                              Staff
                              wrote on last edited by
                              #14

                              @taowang there are browser extensions that allow you to disable CSP. Maybe you can quickly check with those if that is the issue?

                              1 Reply Last reply
                              0
                              • T Offline
                                T Offline
                                taowang
                                wrote on last edited by
                                #15

                                @joseph
                                I just tested the following 4 csp blockers but did not work.
                                The chatbot is still not showing up.
                                I am not sure if these plugins are actually good quality.
                                Some comments say it works, while other say it doesn't work.

                                https://chromewebstore.google.com/detail/disable-content-security/ieelmcmcagommplceebfedjlakkhpden
                                https://chromewebstore.google.com/detail/anti-cors-anti-csp/fcbmpcbjjphnaohicmhefjihollidgkp
                                https://chromewebstore.google.com/detail/allow-csp-content-securit/hnojoemndpdjofcdaonbefcfecpjfflh
                                https://chromewebstore.google.com/detail/csp-unblock/lkbelpgpclajeekijigjffllhigbhobd

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  joseph
                                  Staff
                                  wrote on last edited by
                                  #16

                                  @taowang I tested from your video. From the browser's source it seems that the links are convereted into a tags. In Page -> More -> Filter, if you set "Convert URLs into links and images" to Off, something loads. I can send Hi to chatbot and it returns something.

                                  1 Reply Last reply
                                  0
                                  • T Offline
                                    T Offline
                                    taowang
                                    wrote on last edited by taowang
                                    #17

                                    OMG it worked!!!! Thank you very much.

                                    Here is a site-wide config for all course pages:

                                    1. go to site administration > plugins
                                    2. press command + 5 to search for "Convert URLs into links and images"
                                    3. click on settings and toggle off the HTML format
                                    4. use HTML format when injecting code with urls
                                    1 Reply Last reply
                                    0
                                    • T Offline
                                      T Offline
                                      taowang
                                      wrote on last edited by
                                      #18

                                      image.png

                                      1 Reply Last reply
                                      0
                                      • J joseph has marked this topic as solved on
                                      • J joseph referenced this topic on
                                      • T taowang referenced this topic on
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Bookmarks
                                      • Search