Link and execute external JavaScript file hosted on GitHub

前端 未结 15 849
感动是毒
感动是毒 2020-11-22 04:08

When I try to change the linked reference of a local JavaScript file to a GitHub raw version my test file stops working. The error is:

Refused to exe

相关标签:
15条回答
  • 2020-11-22 04:09

    To make things clear and short

    //raw.githubusercontent.com --> //rawgit.com

    Note that this is handled by rawgit's development hosting and not their cdn for production hosting

    0 讨论(0)
  • 2020-11-22 04:09

    Most simple way:
    <script type="text/plain" src="http://raw.githubusercontent.com/user/repo/branch/file.js"></script>
    Served by GitHub,
    and

    very

    reliable.
    With text/plain
    Without text/plain

    0 讨论(0)
  • 2020-11-22 04:11

    Example


    original

    https://raw.githubusercontent.com/antelove19/qrcodejs/master/qrcode.min.js
    

    cdn.jsdelivr.net

    https://cdn.jsdelivr.net/gh/antelove19/qrcodejs/qrcode.min.js
    
    0 讨论(0)
  • 2020-11-22 04:12

    I found the error was shown due to the comments at the beginning of file , You can solve this issue , by simply creating your own file without comment and push to git, it shows no error

    For proof you can try these two file with same code of easy pagination :

    without comment

    with comment

    0 讨论(0)
  • 2020-11-22 04:15

    raw.github.com is not truely raw access to file asset, but a view rendered by Rails. So accessing raw.github.com is much heavier than needed. I don't know why raw.github.com is implemented as a Rails view. Instead of fix this route issue, GitHub added a X-Content-Type-Options: nosniff header.

    Workaround:

    • Put the script to user.github.io/repo
    • Use a third party CDN like rawgit.com.
    0 讨论(0)
  • 2020-11-22 04:17

    This is no longer possible. GitHub has explicitly disabled JavaScript hotlinking, and newer versions of browsers respect that setting.

    Heads up: nosniff header support coming to Chrome and Firefox

    0 讨论(0)
提交回复
热议问题