Reference GitHub file in jsFiddle

前端 未结 6 941
悲&欢浪女
悲&欢浪女 2020-11-30 18:36

Is there a possibility to misuse grab files from a github repo as external resources in jsFiddle?

相关标签:
6条回答
  • 2020-11-30 18:44

    Nowadays JSDelivr seems to be the best option.

    0 讨论(0)
  • 2020-11-30 18:47

    This is an updated answer, since the url's have changed slightly for Github... I ran into this issue and figured it out for present day. Hopefully this helps people out finding this post recently. Example for Bootstrap Slate theme from Bootswatch:

    1. Raw file url: https://raw2.github.com/thomaspark/bootswatch/gh-pages/slate/bootstrap.css

    2. Remove the 2. after raw: https://rawgithub.com/thomaspark/bootswatch/gh-pages/slate/bootstrap.css

    That's it! :D

    0 讨论(0)
  • 2020-11-30 18:53

    UPDATE

    How to use or misuse github as kind of a CDN is not a thought that only benign "fiddlers" have; criminals have that thought also. Unfortunately, github, being as free and as anonymous as it is, is prone to be misused. As far as I can tell, the fact that some of the above solutions are things which are now broken, has to do with that.

    Here is how I do it. It works now (Nov 2019), but it's admittedly not very convenient.

    Get a github account yourself, if you don't already have one. Create a repository the name of which is identical to your github user name. That repo (and only that repo), - I'll call it the "home repo" - you can use as your web hosting service. https : // yourGithubUserName .github.io will show your home repo "raw/as it is" to the public. (Folder contents is not shown, and you HAVE TO have an index.html)

    Now, if you want to use someone else's github repo in a fiddle, just copy over the complete repo to your home repo, and then just reference your copy of that repo with the src attribute of a script tag in the HTML part of the fiddle. Like this:

    <head>
        <script src="https://mathheadinclouds.github.io/thirdparty/esprima.js"></script>
        <script src="https://mathheadinclouds.github.io/thirdparty/estraverse.browser.js"></script>
        <script src="https://mathheadinclouds.github.io/thirdparty/escope.browser.js"></script>
    </head>
    <body></body>
    

    Above snippet shows the HTML part of a working fiddle which is using the node modules esprima, estraverse, and escope, which is to say, the github repos of the same name. thirdparty is there because that's the name of the subfolder (in my home repo) where I put the copies.

    As I said, not very convenient (lot's of copy and paste to set it all up), but that's what works for me.

    And I should mention, just copy/paste might not be enough, you might have to do browserify or webpack on the referenced repo (if it was made for node, that is.)

    here is the fiddle I was talking about.

    OLD ANSWER (works, but is kind of slow)

    You can use requirify. It's made to enable you to require (as it is in node) on the browser command line; but it works in fiddles too, I tested it. I have no clue if it's "the best", compared to the other methods above (since I didn't go through them all and tested them), but it works.

    Here is an example fiddle loading esprima (javascript parser), then escodegen (reverse javascript parser depending on esprima), then parsing and regenerating some simple javascript code.

    what

    require('lorem', 'ipsum')
    

    does is, it loads the ipsum node module from npm, and puts the result into global variable named lorem. So this is only for npm modules not general github files which aren't also node modules. Shouldn't be much restriction since you can always turn it into a node module if it's your own project.

    here it is

    second example using same technique.

    (((it's actually even simpler as shown in the fiddle. You can just put the 2 require statements right one after the other, you don't need a callback function in between (just one callback function to wait until both are loaded))))

    0 讨论(0)
  • 2020-11-30 18:54

    Another possibility is to add the Git library to the cdnJS Script Repository (they write that any library that is resonably popular on GitHub will be accepted) and then use it as external resource.

    Just found out: there are lots of Javascript libraries at http://jsdb.io/ and it's very easy to add new ones there--i's just a matter of entering the URL of a Github repository.

    0 讨论(0)
  • 2020-11-30 18:56

    If there is a git repo in following folder structure

    fiddletest/test1 (fiddletest is the repo name and test1 is a folder)

    then the corresponding jsfiddle link will be

    http://jsfiddle.net/gh/get/<library name>/<version>/<github user name>/fiddletest/tree/master/test1/
    

    The folder and file structure must be like this

    fiddletest(the repo name)
    |____ test1
          |____ demo.html
          |____ demo.js
          |____ demo.css
          |____ demo.details 
    

    except these three files others will be ignored. the details file should hold the fiddle details and link of external resources(if any) as follows

    ---
    name: test fiddle repo
    description: this is a test repo
    resources: 
      - http://abc.xyz.com/abc.js
      - http://abc.xyz.com/abc2.js
    ...
    

    May be you have noticed the and in the fiddle link. If a fiddle is with pure js the the library name should be "library" and the version should be "pure"

    In a nutshell the fiddle link to reffer to github should be in following format

    http://jsfiddle.net/gh/get/<library name>/<version>/<github user name>/<repo name>/tree/<branchname>/<folder name>/
    
    0 讨论(0)
  • 2020-11-30 18:57

    TLDR; Visit rawgit.com which will pop your files on a CDN straight from GitHub so you can use them.

    Unfortunately none of the answers here worked for me. The rawgithub URL didn't seem to work as the connection gets refused. So here's a full solution that did work. Firstly in GitHub you need to click the Raw button to get the original JavaScript.

    enter image description here

    Then copy the URL from the page it takes you too. You'll notice if you try and use this directly you'll get a warning from JSFiddle.

    enter image description here

    More to the point is the browser will give you an error, e.g.:

    Refused to execute script from https://raw.githubusercontent.com/nnnick/Chart.js/master/Chart.min.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.

    Take that URL and visit rawgit.com. This will give you a URL of the format https://rawgit.com/nnnick/Chart.js/master/Chart.min.js which you can then use.

    I've tried and tested this and it seems to work fine without issue

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