Giter Club home page Giter Club logo

axios-userscript-adapter's Introduction

axios-userscript-adapter

Deprecated

axios 1.0.0 stop exporting utils in its lib directory, it only exporting its axios class now. It will be painfully for me to maintain this adapter for axios>=1.0.0.

Consider to use a fetch api provided by https://github.com/trim21/gm-fetch

Description

An adapter for axios to make ajax calls within UserScript via the GM.xmlHttpRequest function as provided by the Greasemonkey and Tampermonkey WebExtensions for Firefox and Chromium-based browsers.

Synopsis

// ==UserScript==
// @name        new user script
// @namespace   https://trim21.me/
// @description hello
// @version     0.0.1
// @author      Trim21 <[email protected]>
// @match       http*://*/*
// @require     https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js
// @require     https://cdn.jsdelivr.net/npm/[email protected]
// @grant       GM.xmlHttpRequest
// @run-at      document-end
// @connect     httpbin.org
// ==/UserScript==

console.log(axios.defaults.adapter);
axios.defaults.adapter = axiosGmxhrAdapter;

axios.get("https://httpbin.org/headers").then((res) => console.log(res.data));

Description

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 http and https.

Custom adapters are typically used for 'mocking' requests for testing purposes, such as axios-mock-adapter.

axios-userscript-adapter is specifically for using axios in the browser.

Chiefly, in userscript where the xmlHttpRequest function for making ajax requests is replaced with GM.xmlHttpRequest. GM.xmlHttpRequest is a privileged function available within the Greasemonkey and Tampermonkey 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.

After assigning axios-userscript-adapter as the default adapter:

var axios = require("axios");
var adapter = require("axios-userscript-adapter");

axios.defaults.adapter = adapter;

all the usual axios goodness is available within your userscript.

Requirements

axios-userscript-adapter requires axios 0.21.0 or higher

add // @grant GM.xmlHttpRequest to your userscript metadata

If you are using older version, you may grant GM_xmlhttpRequest

Installation:

npm install axios axios-userscript-adapter

Further Examples

As previously shown, you can set axios-userscript-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.

console.log(axios.defaults.adapter);
axios.defaults.adapter = axiosGmxhrAdapter;

axios.get("https://httpbin.org/headers");

Example with webpack:

var axios = require("axios");
var adapter = require("axios-userscript-adapter");

// Send a POST request using GM.xmlHttpRequest
axios({
  method: "post",
  url: "https://www.different-server.com/user/12345",
  data: {
    firstName: "Fred",
    lastName: "Flintstone",
  },
  adapter: adapter,
});

or via any requests made by an instance:

var axios = require("axios");
var adapter = require("axios-userscript-adapter");

var instance = axios.create({
  // `url` is the server URL that will be used for the request
  url: "https://www.different-server.com/user",

  // `method` is the request method to be used when making the request
  method: "post",

  // `adapter` allows custom handling of requests which makes testing easier.
  // Return a promise and supply a valid response.
  adapter: adapter,
});

// Send a POST request using GM.xmlHttpRequest
instance.request({
  data: {
    firstName: "Fred",
    lastName: "Flintstone",
  },
});

esm is also supported:

import adapter from "axios-userscript-adapter";

thanks

axios-userscript-adapter is forked from axios-gmxhr-adapter

Licence

MIT

axios-userscript-adapter's People

Contributors

trim21 avatar renovate[bot] avatar damoclark avatar renovate-bot avatar dependabot[bot] avatar mixa3607 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.