Link and execute external JavaScript file hosted on GitHub

前端 未结 15 879
感动是毒
感动是毒 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条回答
  •  旧时难觅i
    2020-11-22 04:32

    Above answers clearly answer the question but I want to provide another alternative - A different view/approach to solve the similar problem.

    You can also use browser extension to remove X-Content-Type-Options response header for raw.githubusercontent.com files. There are couple of browser extensions to modify response headers.

    1. Requestly: Chrome + Firefox
    2. Modify Headers: Firefox

    If you use Requestly, I can suggest two solutions

    Solution 1: Use Modify Headers Rule and remove the response header

    Steps

    1. Install Requestly from http://www.requestly.in
    2. Go to Rules Page
    3. Click on Add Icon to create a rule
    4. Select Modify Headers
    5. Give a Name and Descripton
    6. Select Remove -> Response -> X-Content-Type-Options
    7. In Source field, enter Url -> Contains -> raw.githubusercontent.com

    Solution 2: Use Replace host Rule

    1. Install Requestly from http://www.requestly.in
    2. Go to Rules Page
    3. Click on Add Icon to create a rule
    4. Replace raw.githubusercontent.com with rawgit.com

    Check this screenshot for more details

    How to test

    We created a simple JS Fiddle to test out if we can use raw github files as scripts in our code. Here is the Fiddle with the following code

    If you see Script evaluated successfully!, It means you are able to use raw github file in your code Otherwise Problem evaluating script indicates that there is some problem while executing script from raw github source.

    I also wrote an article on Requestly blog about this. Please refer it for more details.

    Hope it helps!!

    Disclaimer: I am author of Requestly So you can blame for anything you don't like.

提交回复
热议问题