@xingyuelongchen/vue3-storage
Version:
浏览器Storage使用插件, Storage! Good!
538 lines (473 loc) • 18.4 kB
JavaScript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["index"] = factory();
else
root["index"] = factory();
})((typeof self !== 'undefined' ? self : this), function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "fb15");
/******/ })
/************************************************************************/
/******/ ({
/***/ "1291":
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return setter; });
let option = {};
const setter = config => {
option = config;
};
/* harmony default export */ __webpack_exports__["a"] = (() => {
return option;
});
/***/ }),
/***/ "16db":
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "validatenull", function() { return validatenull; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "setStore", function() { return setStore; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getStore", function() { return getStore; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeStore", function() { return removeStore; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getAllStore", function() { return getAllStore; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "clearStore", function() { return clearStore; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "clearAllStore", function() { return clearAllStore; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "setCookie", function() { return setCookie; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getCookie", function() { return getCookie; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "setsCookie", function() { return setsCookie; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "getsCookie", function() { return getsCookie; });
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "clearCookie", function() { return clearCookie; });
/* harmony import */ var _config__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("1291");
/* eslint-disable */
let {
key = "",
domain = '',
maxage = '',
path = ""
} = _config__WEBPACK_IMPORTED_MODULE_0__[/* default */ "a"];
const keyName = key || Object({"NODE_ENV":"production","BASE_URL":"/"}).VUE_APP_KEY || 'xingyue';
const DOMAIN = domain || Object({"NODE_ENV":"production","BASE_URL":"/"}).VUE_APP_BASE_URL || '';
const PATH = path || Object({"NODE_ENV":"production","BASE_URL":"/"}).VUE_APP_PATH || '/';
const MAXAGE = maxage || Object({"NODE_ENV":"production","BASE_URL":"/"}).VUE_APP_MAXAGE || 1 * 24 * 60 * 60;
/**
* 判断是否为空
*/
function validatenull(val) {
if (typeof val == "boolean") {
return false;
}
if (typeof val == "number") {
return false;
}
if (val instanceof Array) {
if (val.length == 0) return true;
} else if (val instanceof Object) {
if (JSON.stringify(val) === "{}") return true;
} else {
if (val == "null" || val == null || val == "undefined" || val == undefined || val == "") return true;
return false;
}
return false;
}
/**
* 设置storage缓存
* @param {String or Object} name
* @param {any} content
* @param {Boolean} type
*/
function setStore(params) {
if (params.constructor === Object) var {
name = null,
content = null,
type = false
} = params;else var [name, content, type = null] = [...arguments];
name = keyName + name;
let obj = {
dataType: typeof content,
content: content,
type: type,
datetime: Date.now()
};
let str = btoa(encodeURIComponent(JSON.stringify(obj)));
if (type) window.sessionStorage.setItem(name, str);else window.localStorage.setItem(name, str);
return true;
}
/**
* 获取Storage
*/
function getStore(params) {
if (params.constructor === Object) var {
name = null,
debug = null
} = params;else var [name, debug = null] = [...arguments];
name = keyName + name;
let obj = {},
content = null;
obj = window.sessionStorage.getItem(name);
if (validatenull(obj)) obj = window.localStorage.getItem(name);
if (validatenull(obj)) return;
obj = decodeURIComponent(atob(obj));
try {
obj = JSON.parse(obj);
} catch (obj) {
return obj;
}
if (debug) {
return obj;
}
if (obj.dataType == 'string') {
content = obj.content;
} else if (obj.dataType == 'number') {
content = Number(obj.content);
} else if (obj.dataType == 'boolean') {
content = eval(obj.content);
} else if (obj.dataType == 'object') {
content = obj.content;
}
return content;
}
/**
* 删除storage
*/
function removeStore(name) {
name = keyName + name;
window.sessionStorage.removeItem(name);
window.localStorage.removeItem(name);
}
/**
* 获取localStorage或sessionStorage
*/
function getAllStore(type) {
let obj = {},
store = '';
if (type) store = window.sessionStorage;else store = window.localStorage;
for (let i = 0; i <= store.length; i++) {
let name = store.key(i);
obj[name] = getStore(name);
}
return obj;
}
/**
* 清空localStorage或sessionStorage
*/
function clearStore(type = false) {
if (type) {
window.sessionStorage.clear();
} else {
window.localStorage.clear();
}
}
/**
* 清空全部Storage
*/
function clearAllStore() {
window.sessionStorage.clear();
window.localStorage.clear();
}
/**
* 设置cookie
* @param {Object} params 包含name和data
*/
function setCookie(params = {}) {
try {
if (params.constructor === Object) var {
name = null,
data = null
} = params;else var [name, data = null] = [...arguments];
name = name;
data = encodeURIComponent(btoa(JSON.stringify({
content: data
})));
let cookie = `${name}=${data}`;
if (MAXAGE) cookie += ';' + MAXAGE;
if (DOMAIN) cookie += ';' + DOMAIN;
if (PATH) cookie += ';' + PATH;
document.cookie = cookie; // `${name}=${data}; Max-Age=${MAXAGE}; domain=${DOMAIN};path=${PATH}`
return true;
} catch (error) {
return false;
}
}
/**
* 获取cookie
*/
function getCookie(name) {
name = name;
let cookie = document.cookie;
let cookies = {};
cookie.split('; ').map(e => e.split('=')).forEach(e => {
cookies[e[0]] = e[1];
});
cookie = cookies[name] ? atob(decodeURIComponent(cookies[name])) : false; // cookie = cookies[name] ? window.atob(cookies[name]) : false;
if (cookie) {
return JSON.parse(cookie).content;
} else {
return false;
}
}
/**
* 设置原生cookie
* @param {Object} params 包含name和data
*/
function setsCookie(params = {}) {
try {
if (params.constructor === Object) var {
name = null,
data = null
} = params;else var [name, data = null] = [...arguments];
name = name;
data = data;
let cookie = `${name}=${data}`;
if (MAXAGE) cookie += ';' + MAXAGE;
if (DOMAIN) cookie += ';' + DOMAIN;
if (PATH) cookie += ';' + PATH;
document.cookie = cookie; // `${name}=${data}; Max-Age=${MAXAGE}; domain=${DOMAIN};path=${PATH}`
return true;
} catch (error) {
return false;
}
}
/**
* 获取原生cookie
*/
function getsCookie(name) {
let cookie = document.cookie;
let cookies = {};
cookie.split('; ').map(e => e.split('=')).forEach(e => {
cookies[e[0]] = e[1];
});
cookie = cookies[name] ? cookies[name] : false;
return cookie;
}
/**
* 删除cookie
* @param {string} name 指定cookie名称,如果为null删除全部cookie
*/
function clearCookie(name = '') {
if (name) document.cookie = `${name}=; Max-Age=0; domain=${DOMAIN};path=${path}`;else {
let cookie = document.cookie;
let cookies = {};
cookie.split('; ').map(e => e.split('=')).forEach(e => {
cookies[e[0]] = e[1];
});
Object.keys(cookies).forEach(e => {
document.cookie = `${e}=; Max-Age=0;`;
});
}
}
/***/ }),
/***/ "8875":
/***/ (function(module, exports, __webpack_require__) {
var __WEBPACK_AMD_DEFINE_FACTORY__, __WEBPACK_AMD_DEFINE_ARRAY__, __WEBPACK_AMD_DEFINE_RESULT__;// addapted from the document.currentScript polyfill by Adam Miller
// MIT license
// source: https://github.com/amiller-gh/currentScript-polyfill
// added support for Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1620505
(function (root, factory) {
if (true) {
!(__WEBPACK_AMD_DEFINE_ARRAY__ = [], __WEBPACK_AMD_DEFINE_FACTORY__ = (factory),
__WEBPACK_AMD_DEFINE_RESULT__ = (typeof __WEBPACK_AMD_DEFINE_FACTORY__ === 'function' ?
(__WEBPACK_AMD_DEFINE_FACTORY__.apply(exports, __WEBPACK_AMD_DEFINE_ARRAY__)) : __WEBPACK_AMD_DEFINE_FACTORY__),
__WEBPACK_AMD_DEFINE_RESULT__ !== undefined && (module.exports = __WEBPACK_AMD_DEFINE_RESULT__));
} else {}
}(typeof self !== 'undefined' ? self : this, function () {
function getCurrentScript () {
var descriptor = Object.getOwnPropertyDescriptor(document, 'currentScript')
// for chrome
if (!descriptor && 'currentScript' in document && document.currentScript) {
return document.currentScript
}
// for other browsers with native support for currentScript
if (descriptor && descriptor.get !== getCurrentScript && document.currentScript) {
return document.currentScript
}
// IE 8-10 support script readyState
// IE 11+ & Firefox support stack trace
try {
throw new Error();
}
catch (err) {
// Find the second match for the "at" string to get file src url from stack.
var ieStackRegExp = /.*at [^(]*\((.*):(.+):(.+)\)$/ig,
ffStackRegExp = /@([^@]*):(\d+):(\d+)\s*$/ig,
stackDetails = ieStackRegExp.exec(err.stack) || ffStackRegExp.exec(err.stack),
scriptLocation = (stackDetails && stackDetails[1]) || false,
line = (stackDetails && stackDetails[2]) || false,
currentLocation = document.location.href.replace(document.location.hash, ''),
pageSource,
inlineScriptSourceRegExp,
inlineScriptSource,
scripts = document.getElementsByTagName('script'); // Live NodeList collection
if (scriptLocation === currentLocation) {
pageSource = document.documentElement.outerHTML;
inlineScriptSourceRegExp = new RegExp('(?:[^\\n]+?\\n){0,' + (line - 2) + '}[^<]*<script>([\\d\\D]*?)<\\/script>[\\d\\D]*', 'i');
inlineScriptSource = pageSource.replace(inlineScriptSourceRegExp, '$1').trim();
}
for (var i = 0; i < scripts.length; i++) {
// If ready state is interactive, return the script tag
if (scripts[i].readyState === 'interactive') {
return scripts[i];
}
// If src matches, return the script tag
if (scripts[i].src === scriptLocation) {
return scripts[i];
}
// If inline source matches, return the script tag
if (
scriptLocation === currentLocation &&
scripts[i].innerHTML &&
scripts[i].innerHTML.trim() === inlineScriptSource
) {
return scripts[i];
}
}
// If no match, return null
return null;
}
};
return getCurrentScript
}));
/***/ }),
/***/ "fb15":
/***/ (function(module, __webpack_exports__, __webpack_require__) {
;
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// EXPORTS
__webpack_require__.d(__webpack_exports__, "validatenull", function() { return /* reexport */ storage["validatenull"]; });
__webpack_require__.d(__webpack_exports__, "setStore", function() { return /* reexport */ storage["setStore"]; });
__webpack_require__.d(__webpack_exports__, "getStore", function() { return /* reexport */ storage["getStore"]; });
__webpack_require__.d(__webpack_exports__, "removeStore", function() { return /* reexport */ storage["removeStore"]; });
__webpack_require__.d(__webpack_exports__, "getAllStore", function() { return /* reexport */ storage["getAllStore"]; });
__webpack_require__.d(__webpack_exports__, "clearStore", function() { return /* reexport */ storage["clearStore"]; });
__webpack_require__.d(__webpack_exports__, "clearAllStore", function() { return /* reexport */ storage["clearAllStore"]; });
__webpack_require__.d(__webpack_exports__, "setCookie", function() { return /* reexport */ storage["setCookie"]; });
__webpack_require__.d(__webpack_exports__, "getCookie", function() { return /* reexport */ storage["getCookie"]; });
__webpack_require__.d(__webpack_exports__, "setsCookie", function() { return /* reexport */ storage["setsCookie"]; });
__webpack_require__.d(__webpack_exports__, "getsCookie", function() { return /* reexport */ storage["getsCookie"]; });
__webpack_require__.d(__webpack_exports__, "clearCookie", function() { return /* reexport */ storage["clearCookie"]; });
// CONCATENATED MODULE: ./node_modules/@vue/cli-service/lib/commands/build/setPublicPath.js
// This file is imported into lib/wc client bundles.
if (typeof window !== 'undefined') {
var currentScript = window.document.currentScript
if (true) {
var getCurrentScript = __webpack_require__("8875")
currentScript = getCurrentScript()
// for backward compatibility, because previously we directly included the polyfill
if (!('currentScript' in document)) {
Object.defineProperty(document, 'currentScript', { get: getCurrentScript })
}
}
var src = currentScript && currentScript.src.match(/(.+\/)[^/]+\.js(\?.*)?$/)
if (src) {
__webpack_require__.p = src[1] // eslint-disable-line
}
}
// Indicate to webpack that this file can be concatenated
/* harmony default export */ var setPublicPath = (null);
// EXTERNAL MODULE: ./utils/packages/config.js
var config = __webpack_require__("1291");
// EXTERNAL MODULE: ./utils/packages/storage.js
var storage = __webpack_require__("16db");
// CONCATENATED MODULE: ./utils/index.js
/* harmony default export */ var utils = ({
install: (App, option) => {
Object(config["b" /* setter */])(option);
let obj = { ...storage
};
Object.keys(obj).forEach(e => {
if (/^3/.test(App.version)) App.config.globalProperties['$' + e] = obj[e];
if (/^2/.test(App.version)) App.prototype['$' + e] = obj[e];
});
}
});
// CONCATENATED MODULE: ./node_modules/@vue/cli-service/lib/commands/build/entry-lib.js
/* harmony default export */ var entry_lib = __webpack_exports__["default"] = (utils);
/***/ })
/******/ });
});