Coder Social home page Coder Social logo

pruvonet / cordova-network-interceptor Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 18 KB

network interceptor for cordova app that will fix all protocol relative url requests to use https

License: GNU General Public License v3.0

TypeScript 100.00%
cordova interceptor file plugin network protocol relative request javascript

cordova-network-interceptor's Introduction

Build Status

cordova-network-interceptor

Network interceptor for cordova app that will fix all protocol relative url requests to use https

Cordova is essentially a web view that serves files from the file system. As such, the protocl of the "url" it uses is file: (e.g. file://android_assets/www/index.html).

It is a common practice when writing modules, to reference urls to be used using the "relative protocol" // (e.g. //www.googleapis.com/widget.js).

In cordova, this causes the url to be fetched with the file: protocol, which causes faliures and unloaded external libraries.
For example, //www.googleapis.com/widget.js will be translated to file://www.googleapis.com/widget.js.

To overcome the issue, the library will monkey patch a few things to force the urls to be using the https: protocol.

The library performs 4 operations:

  1. Patch the document.createElement method, which is used (amongst other things) to add dynamic scripts to be fetched by the browser. The patch will alter the script src property if needed to use a proper protocol.
  2. Patch the Image constructor, which is commonly used to fire pixels. The patch will alter the image src property if needed to use a proper protocol.
  3. Patch the XMLHttpRequest object, to alter the url if needed to use a proper protocol, before the request is being executed.
  4. Patch the fetch API, to alter the url if needed to use a proper protocol, before the request is being executed.

So, for example, if any script, no matter how fetched and when, will use //www.googleapis.com/widget.js as its source, it will be translated and fetched by the https://www.googleapis.com/widget.js correct url.

Installation

npm install cordova-network-interceptor --save

or

yarn add cordova-network-interceptor

Usage

Add at the TOP of the <head> element

<script src="node_modules/cordova-network-interceptor/dist/index.js"></script>

Credits

The code introduced here is inspired by a mesh up of code examples taken from:

cordova-network-interceptor's People

Contributors

regevbr avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.