├── sample ├── worker.js └── index.html ├── LICENSE.txt ├── twix.min.js ├── README.md └── twix.js /sample/worker.js: -------------------------------------------------------------------------------- 1 | importScripts('../twix.min.js'); 2 | 3 | getTime(); 4 | setInterval(getTime, 1000); 5 | 6 | function getTime() { 7 | Twix.ajax({ 8 | url: "https://neilcowburn.com/api/test.php", 9 | success: function(data) { 10 | postMessage(data); 11 | } 12 | }); 13 | } 14 | -------------------------------------------------------------------------------- /sample/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Twix Sample 5 | 6 | 7 | 8 |
9 | 10 | 21 | 22 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Neil Cowburn (http://github.com/neilco/) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /twix.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Twix v1.0 - a lightweight library for making AJAX requests. 3 | * Author: Neil Cowburn (neilco@gmail.com) 4 | * 5 | * Copyright (c) 2013 Neil Cowburn (http://github.com/neilco/) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | var Twix=function(){function t(){}t.ajax=function(t){t=t||{url:""},t.type=t.type&&t.type.toUpperCase()||"GET",t.headers=t.headers||{},t.timeout=parseInt(t.timeout)||0,t.success=t.success||function(){},t.error=t.error||function(){},t.async="undefined"==typeof t.async?!0:t.async;var e=new XMLHttpRequest;t.timeout>0&&(e.timeout=t.timeout,e.ontimeout=function(){t.error("timeout","timeout",e)}),e.open(t.type,t.url,t.async);for(var s in t.headers)t.headers.hasOwnProperty(s)&&e.setRequestHeader(s,t.headers[s]);return e.send(t.data),e.onreadystatechange=function(){if(4==this.readyState&&(this.status>=200&&this.status<300||304==this.status)){var e=this.responseText,s=this.getResponseHeader("Content-Type");s&&s.match(/json/)&&(e=JSON.parse(this.responseText)),t.success(e,this.statusText,this)}else 4==this.readyState&&t.error(this.status,this.statusText,this)},0==t.async&&(4==e.readyState&&(e.status>=200&&e.status<300||304==e.status)?t.success(e.responseText,e):4==e.readyState&&t.error(e.status,e.statusText,e)),e};var e=function(e,s,n,r){return"function"==typeof n&&(r=n,n=void 0),t.ajax({url:s,data:n,type:e,success:r})};return t.get=function(t,s,n){return e("GET",t,s,n)},t.head=function(t,s,n){return e("HEAD",t,s,n)},t.post=function(t,s,n){return e("POST",t,s,n)},t.patch=function(t,s,n){return e("PATCH",t,s,n)},t.put=function(t,s,n){return e("PUT",t,s,n)},t["delete"]=function(t,s,n){return e("DELETE",t,s,n)},t.options=function(t,s,n){return e("OPTIONS",t,s,n)},t}();__=Twix; 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Twix 2 | 3 | __Twix__ is a lightweight JavaScript library for making AJAX requests in places where jQuery won't go. 4 | 5 | ### ★ Don't let its tiny size deceive you ★ 6 | 7 | It may weigh-in at just over 1KB, but this puppy has teeth. __WOOF!__ 8 | 9 | Modelled after the ubiquitious jQuery's AJAX methods (_$.ajax_, _$.get_, _$.post_, _$.patch, _$.put_, _$.delete_ and _$.options_), you can use this in places were jQuery will not load, such as __inside a WebWorker__#. You little ripper! 10 | 11 | You may be like me and occasionally think that loading the entire jQuery library (about 80KB minified) just to make an AJAX call is just like filling a thimble using a fire hydrant. __Twix__ is written from the ground up to work in modern browsers (IE9+, Firefox, Chrome, Safari, etc. Anything that supports __XMLHttpRequest__, really) and has just enough magic sauce to get the job done. No more, no less. 12 | 13 | --- 14 | 15 | # Actually, providing a simple way to make make AJAX calls inside a WebWorker was the primary use-case when developing this golden nugget. How about that. 16 | 17 | ## Usage 18 | 19 | Simply add a reference to __twix.js__ or __twix.min.js__ and you're good to go. 20 | 21 | > <script type="text/javascript" src="twix.min.js"></script> 22 | 23 | If you want to make an AJAX call from within a WebWorker, load the __Twix__ library using the __importScripts__ function: 24 | 25 | > importScripts("twix.min.js"); 26 | 27 | Making an AJAX request is as simple as calling __Twix.ajax(...)__ or __Twix.get(...)__ or __Twix.post(...)__. If typing the four characters of __Twix__ is too much, you use the optional double-underscore (*__.ajax(...)*) shortcut. 28 | 29 | There's an _ultra-basic_ sample of making a GET request from a WebWorker in the [__sample__](http://github.com/neilco/twix/sample/) directory. 30 | 31 | ## Contributors 32 | 33 | Well, it's just [__me__](http://github.com/neilco/). It's a little lonely, so feel free to issue a pull request with fixes or enhancements and get your name listed here. 34 | 35 | 36 | ## Why Twix? 37 | 38 | Because [two chocolatey fingers](http://www.twix.com) are better than one. 39 | 40 | ## License 41 | 42 | [MIT license](http://neil.mit-license.org) 43 | 44 | Copyright (c) 2013 Neil Cowburn (http://github.com/neilco/) 45 | 46 | Permission is hereby granted, free of charge, to any person obtaining a copy 47 | of this software and associated documentation files (the "Software"), to deal 48 | in the Software without restriction, including without limitation the rights 49 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 50 | copies of the Software, and to permit persons to whom the Software is 51 | furnished to do so, subject to the following conditions: 52 | 53 | The above copyright notice and this permission notice shall be included in 54 | all copies or substantial portions of the Software. 55 | 56 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 57 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 58 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 59 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 60 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 61 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 62 | THE SOFTWARE. 63 | -------------------------------------------------------------------------------- /twix.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Twix v1.0 - a lightweight library for making AJAX requests. 3 | * Author: Neil Cowburn (neilco@gmail.com) 4 | * 5 | * Copyright (c) 2013 Neil Cowburn (http://github.com/neilco/) 6 | * 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy 8 | * of this software and associated documentation files (the "Software"), to deal 9 | * in the Software without restriction, including without limitation the rights 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | * copies of the Software, and to permit persons to whom the Software is 12 | * furnished to do so, subject to the following conditions: 13 | * 14 | * The above copyright notice and this permission notice shall be included in 15 | * all copies or substantial portions of the Software. 16 | * 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | * THE SOFTWARE. 24 | */ 25 | var Twix = (function () { 26 | function Twix() { } 27 | 28 | Twix.ajax = function(options) { 29 | options = options || {url:""}; 30 | options.type = options.type && options.type.toUpperCase() || 'GET'; 31 | options.headers = options.headers || {}; 32 | options.timeout = parseInt(options.timeout) || 0; 33 | options.success = options.success || function() {}; 34 | options.error = options.error || function() {}; 35 | options.async = typeof options.async === 'undefined' ? true : options.async; 36 | 37 | var client = new XMLHttpRequest(); 38 | if (options.timeout > 0) { 39 | client.timeout = options.timeout; 40 | client.ontimeout = function () { 41 | options.error('timeout', 'timeout', client); 42 | }; 43 | } 44 | client.open(options.type, options.url, options.async); 45 | 46 | for (var i in options.headers) { 47 | if (options.headers.hasOwnProperty(i)) { 48 | client.setRequestHeader(i, options.headers[i]); 49 | } 50 | } 51 | 52 | client.send(options.data); 53 | client.onreadystatechange = function() { 54 | if (this.readyState == 4 && ((this.status >= 200 && this.status < 300) || this.status == 304)) { 55 | var data = this.responseText; 56 | var contentType = this.getResponseHeader('Content-Type'); 57 | if (contentType && contentType.match(/json/)) { 58 | data = JSON.parse(this.responseText); 59 | } 60 | options.success(data, this.statusText, this); 61 | } else if (this.readyState == 4) { 62 | options.error(this.status, this.statusText, this); 63 | } 64 | }; 65 | 66 | if (options.async == false) { 67 | if (client.readyState == 4 && ((client.status >= 200 && client.status < 300) || client.status == 304)) { 68 | options.success(client.responseText, client); 69 | } else if (client.readyState == 4) { 70 | options.error(client.status, client.statusText, client); 71 | } 72 | } 73 | 74 | return client; 75 | }; 76 | 77 | var _ajax = function(type, url, data, callback) { 78 | if (typeof data === "function") { 79 | callback = data; 80 | data = undefined; 81 | } 82 | return Twix.ajax({ 83 | url: url, 84 | data: data, 85 | type: type, 86 | success: callback 87 | }); 88 | }; 89 | 90 | Twix.get = function(url, data, callback) { 91 | return _ajax("GET", url, data, callback); 92 | }; 93 | 94 | Twix.head = function(url, data, callback) { 95 | return _ajax("HEAD", url, data, callback); 96 | }; 97 | 98 | Twix.post = function(url, data, callback) { 99 | return _ajax("POST", url, data, callback); 100 | }; 101 | 102 | Twix.patch = function(url, data, callback) { 103 | return _ajax("PATCH", url, data, callback); 104 | }; 105 | 106 | Twix.put = function(url, data, callback) { 107 | return _ajax("PUT", url, data, callback); 108 | }; 109 | 110 | Twix.delete = function(url, data, callback) { 111 | return _ajax("DELETE", url, data, callback); 112 | }; 113 | 114 | Twix.options = function(url, data, callback) { 115 | return _ajax("OPTIONS", url, data, callback); 116 | }; 117 | 118 | 119 | return Twix; 120 | })(); 121 | 122 | __ = Twix; 123 | --------------------------------------------------------------------------------