var axios = ;var adapter = ;axiosdefaultsadapter = adapter ;// Make a request for a user with a given ID using GM_xmlhttpRequest// crossing same origin policy boundariesaxios;
The axios documentation describes axios adapters as modules that handle dispatching a request and settling a returned Promise once a response is received. The standard axios distribution includes adapters for the browser via
xmlHttpRequest, and node.js via
Custom adapters are typically used for 'mocking' requests for testing purposes, such as axios-mock-adapter.
axios-gmxhr-adapter is specifically for using axios in the browser, chiefly, in userscripts where the
xmlHttpRequest function for making ajax requests is replaced with
GM_xmlhttpRequest is a privileged function available within the Greasemonkey and
webextensions that allow userscripts to make ajax requests that cross same origin policy boundaries. In other words, using axios, the userscript can make http requests to sites that didn't originate from the currently loaded web page. Read the GM_xmlhttpRequest function wiki page for further details.
axios-gmxhr-adapter as the default adapter:
var axios = ;var adapter = ;axiosdefaultsadapter = adapter ;
all the usual axios goodness is available within your userscript.
axios-gmxhr-adapter requires axios 0.13.0 or higher
npm install axios axios-gmxhr-adapter
As previously shown, you can set
axios-gmxhr-adapter as the default adapter, in which case, all axios requests will be dispatched via
GM_xmlhttpRequest. However, you can instead specify the adapter on individual requests via a
config object. For example:
var axios = ;var adapter = ;// Send a POST request using GM_xmlhttpRequest;
or via any requests made by an instance:
var axios = ;var adapter = ;var instance = axios;// Send a POST request using GM_xmlhttpRequestinstance;
You may also be interested in the following modules to assist with userscripting:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DAMIEN CLARK BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.