Giter Club home page Giter Club logo

cross-data's Introduction

cross-data

跨域数据共享方案,突破浏览器同源限制,共享localStorage数据;

安装

npm install --save cross-data 
# or
yarn add cross-data

实现效果

cross-data

使用方法

  1. 创建hub.html

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Hub</title>
    
    </head>
    
    <body>
    
        <script src="../dist/hub.js"></script>
    
        <script>
            new Hub(safeDomain, prefix);
    
        </script>
    </body>
    
    </html>
  2. 项目中添加client

    import Client from 'cross-data/dist/client.js';
    
    const client = new Client({
        iframeUrl: 'https://test.example.com/hub.html'  // hub.html文件的目录
    })
    
    // 写数据
    client.set(key, value).then(res => {
        console.log('操作成功')
    })
    // 读数据
    client.get(key).then(data => {
        console.log(data)
    })

API介绍

  1. new hub() 参数

    参数名 类型 说明 默认值
    safeDomain RegExp 域名白名单,正则表达式 /.*/
    prefix String key前缀
  2. client API方法

    • set(key, value): 设置数据

    • get(key): 获取数据

    • remove(key): 删除数据

    • clear(): 清空所有数据

    以上api都返回一个Promise对象

cross-data's People

Contributors

k-dylan avatar dependabot[bot] 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.