1
0
mirror of synced 2025-11-06 04:21:11 +08:00

refactor: use chobitsu

This commit is contained in:
surunzi
2020-07-03 14:30:04 +08:00
parent 49db1041b0
commit 78c288174a
10 changed files with 51 additions and 682 deletions

View File

@@ -1,6 +1,6 @@
module.exports = {
eruda: {
library: 'https://raw.githubusercontent.com/liriliri/fione/master/',
library: ['fione', 'node_modules/eustia-module'],
files: 'src/**/*.js',
ignore: 'src/**/stringify.js',
output: 'src/lib/util.js',
@@ -8,6 +8,7 @@ module.exports = {
format: 'es'
},
test: {
library: ['node_modules/eustia-module'],
files: ['test/*.js', 'test/*.html'],
exclude: ['js'],
namespace: 'util',

1
.gitignore vendored
View File

@@ -3,7 +3,6 @@ dist/
node_modules/
test/lib/
coverage/
eustia/
test/playground.html
npm-debug.log
package-lock.json

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "fione"]
path = fione
url = git@github.com:liriliri/fione.git

View File

@@ -32,6 +32,9 @@ const cssMinifierLoader = {
module.exports = {
entry: './src/index',
resolve: {
symlinks: false
},
devServer: {
contentBase: './test',
port: 3000

View File

@@ -363,6 +363,14 @@ Emit event.
|event |Event name |
|...args|Arguments passed to listener|
### removeAllListeners
Remove all listeners.
|Name |Desc |
|-----|----------|
|event|Event name|
### mixin
[static] Mixin object class methods.
@@ -964,46 +972,6 @@ contain({ a: 1, b: 2 }, 1); // -> true
contain('abc', 'a'); // -> true
```
## cookie
Simple api for handling browser cookies.
### get
Get cookie value.
|Name |Desc |
|------|--------------------------|
|key |Cookie key |
|return|Corresponding cookie value|
### set
Set cookie value.
|Name |Desc |
|-------|--------------|
|key |Cookie key |
|val |Cookie value |
|options|Cookie options|
|return |Module cookie |
### remove
Remove cookie value.
|Name |Desc |
|-------|--------------|
|key |Cookie key |
|options|Cookie options|
|return |Module cookie |
```javascript
cookie.set('a', '1', { path: '/' });
cookie.get('a'); // -> '1'
cookie.remove('a');
```
## copy
Copy text to clipboard using document.execCommand.
@@ -1106,20 +1074,6 @@ const calLayout = debounce(function() {}, 300);
// $(window).resize(calLayout);
```
## decodeUriComponent
Better decodeURIComponent that does not throw if input is invalid.
|Name |Desc |
|------|----------------|
|str |String to decode|
|return|Decoded string |
```javascript
decodeUriComponent('%%25%'); // -> '%%%'
decodeUriComponent('%E0%A4%A'); // -> '\xE0\xA4%A'
```
## defaults
Fill in undefined properties in object with the first value present in the following list of defaults objects.
@@ -2449,18 +2403,6 @@ Reverse array without mutating it.
reverse([1, 2, 3]); // -> [3, 2, 1]
```
## rmCookie
Loop through all possible path and domain to remove cookie.
|Name|Desc |
|----|----------|
|key |Cookie key|
```javascript
rmCookie('test');
```
## root
Root object reference, `global` in nodeJs, `window` in browser.
@@ -2768,35 +2710,6 @@ type(async function() {}, false); // -> 'AsyncFunction'
Used for typescript definitions only.
## ucs2
UCS-2 encoding and decoding.
### encode
Create a string using an array of code point values.
|Name |Desc |
|------|--------------------|
|arr |Array of code points|
|return|Encoded string |
### decode
Create an array of code point values using a string.
|Name |Desc |
|------|--------------------|
|str |Input string |
|return|Array of code points|
```javascript
ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc'
ucs2.decode('abc'); // -> [0x61, 0x62, 0x63]
'𝌆'.length; // -> 2
ucs2.decode('𝌆').length; // -> 1
```
## uncaught
Handle global uncaught errors and promise rejections.
@@ -2872,34 +2785,6 @@ Convert the first character of string to upper case.
upperFirst('red'); // -> Red
```
## utf8
UTF-8 encoding and decoding.
### encode
Turn any UTF-8 decoded string into UTF-8 encoded string.
|Name |Desc |
|------|----------------|
|str |String to encode|
|return|Encoded string |
### decode
Turn any UTF-8 encoded string into UTF-8 decoded string.
|Name |Desc |
|----------|----------------------|
|str |String to decode |
|safe=false|Suppress error if true|
|return |Decoded string |
```javascript
utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80'
utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00'
```
## values
Create an array of the own enumerable property values of object.

1
fione Submodule

Submodule fione added at 94408eb8b1

View File

@@ -40,10 +40,12 @@
"autoprefixer": "^9.7.4",
"babel-eslint": "^10.1.0",
"babel-loader": "^8.0.6",
"chobitsu": "^0.2.0",
"css-loader": "^3.4.2",
"draggabilly": "^2.2.0",
"eslint": "^6.8.0",
"eslint-loader": "^3.0.3",
"eustia-module": "^1.23.0",
"handlebars": "^4.7.3",
"handlebars-loader": "^1.7.1",
"html-minifier": "^4.0.0",

View File

@@ -14,15 +14,15 @@ import {
MutationObserver,
toArr,
concat,
rmCookie,
decodeUriComponent,
isNull,
lowerCase,
contain,
filter
filter,
map
} from '../lib/util'
import { isErudaEl } from '../lib/extraUtil'
import evalCss from '../lib/evalCss'
import chobitsu from 'chobitsu'
export default class Resources extends Tool {
constructor() {
@@ -155,20 +155,11 @@ export default class Resources extends Tool {
this['_' + type + 'StoreData'] = storeData
}
refreshCookie() {
const cookieData = []
const cookie = document.cookie
if (trim(cookie) !== '') {
each(document.cookie.split(';'), function(val) {
val = val.split('=')
const key = trim(val.shift())
val = decodeUriComponent(val.join('='))
cookieData.push({
key,
val
})
})
}
const { cookies } = chobitsu.domain('Network').getCookies()
const cookieData = map(cookies, ({ name, value }) => ({
key: name,
val: value
}))
this._cookieData = cookieData
@@ -283,7 +274,7 @@ export default class Resources extends Tool {
.on('click', '.eruda-delete-cookie', function() {
const key = $(this).data('key')
rmCookie(key)
chobitsu.domain('Network').deleteCookies({ name: key })
self.refreshCookie()._render()
})
.on('click', '.eruda-clear-storage', function() {
@@ -300,7 +291,9 @@ export default class Resources extends Tool {
}
})
.on('click', '.eruda-clear-cookie', () => {
each(this._cookieData, val => rmCookie(val.key))
chobitsu.domain('Storage').clearDataForOrigin({
storageTypes: 'cookies'
})
this.refreshCookie()._render()
})
.on('click', '.eruda-storage-val', function() {

View File

@@ -26,6 +26,7 @@ import {
nextTick
} from './lib/util'
import evalCss from './lib/evalCss'
import chobitsu from 'chobitsu'
export default {
init({ container, tool, autoScale = true, useShadowDom = true, defaults = {} } = {}) {
@@ -47,6 +48,7 @@ export default {
_isInit: false,
version: VERSION,
util,
chobitsu,
Tool,
Console,
Elements,

View File

@@ -743,271 +743,6 @@ export var debounce = _.debounce = (function (exports) {
return exports;
})({});
/* ------------------------------ ucs2 ------------------------------ */
export var ucs2 = _.ucs2 = (function (exports) {
/* UCS-2 encoding and decoding.
*
* ### encode
*
* Create a string using an array of code point values.
*
* |Name |Desc |
* |------|--------------------|
* |arr |Array of code points|
* |return|Encoded string |
*
* ### decode
*
* Create an array of code point values using a string.
*
* |Name |Desc |
* |------|--------------------|
* |str |Input string |
* |return|Array of code points|
*/
/* example
* ucs2.encode([0x61, 0x62, 0x63]); // -> 'abc'
* ucs2.decode('abc'); // -> [0x61, 0x62, 0x63]
* '𝌆'.length; // -> 2
* ucs2.decode('𝌆').length; // -> 1
*/
/* typescript
* export declare const ucs2: {
* encode(arr: number[]): string;
* decode(str: string): number[];
* };
*/
// https://mathiasbynens.be/notes/javascript-encoding
exports = {
encode: function(arr) {
return String.fromCodePoint.apply(String, arr);
},
decode: function(str) {
var ret = [];
var i = 0;
var len = str.length;
while (i < len) {
var c = str.charCodeAt(i++); // A high surrogate
if (c >= 0xd800 && c <= 0xdbff && i < len) {
var tail = str.charCodeAt(i++); // nextC >= 0xDC00 && nextC <= 0xDFFF
if ((tail & 0xfc00) === 0xdc00) {
// C = (H - 0xD800) * 0x400 + L - 0xDC00 + 0x10000
ret.push(((c & 0x3ff) << 10) + (tail & 0x3ff) + 0x10000);
} else {
ret.push(c);
i--;
}
} else {
ret.push(c);
}
}
return ret;
}
};
return exports;
})({});
/* ------------------------------ utf8 ------------------------------ */
export var utf8 = _.utf8 = (function (exports) {
/* UTF-8 encoding and decoding.
*
* ### encode
*
* Turn any UTF-8 decoded string into UTF-8 encoded string.
*
* |Name |Desc |
* |------|----------------|
* |str |String to encode|
* |return|Encoded string |
*
* ### decode
*
* Turn any UTF-8 encoded string into UTF-8 decoded string.
*
* |Name |Desc |
* |----------|----------------------|
* |str |String to decode |
* |safe=false|Suppress error if true|
* |return |Decoded string |
*/
/* example
* utf8.encode('\uD800\uDC00'); // -> '\xF0\x90\x80\x80'
* utf8.decode('\xF0\x90\x80\x80'); // -> '\uD800\uDC00'
*/
/* typescript
* export declare const utf8: {
* encode(str: string): string;
* decode(str: string, safe?: boolean): string;
* };
*/
/* dependencies
* ucs2
*/ // https://encoding.spec.whatwg.org/#utf-8
exports = {
encode: function(str) {
var codePoints = ucs2.decode(str);
var byteArr = '';
for (var i = 0, len = codePoints.length; i < len; i++) {
byteArr += encodeCodePoint(codePoints[i]);
}
return byteArr;
},
decode: function(str, safe) {
byteArr = ucs2.decode(str);
byteIdx = 0;
byteCount = byteArr.length;
codePoint = 0;
bytesSeen = 0;
bytesNeeded = 0;
lowerBoundary = 0x80;
upperBoundary = 0xbf;
var codePoints = [];
var tmp;
while ((tmp = decodeCodePoint(safe)) !== false) {
codePoints.push(tmp);
}
return ucs2.encode(codePoints);
}
};
var fromCharCode = String.fromCharCode;
function encodeCodePoint(codePoint) {
// U+0000 to U+0080, ASCII code point
if ((codePoint & 0xffffff80) === 0) {
return fromCharCode(codePoint);
}
var ret = '',
count,
offset; // U+0080 to U+07FF, inclusive
if ((codePoint & 0xfffff800) === 0) {
count = 1;
offset = 0xc0;
} else if ((codePoint & 0xffff0000) === 0) {
// U+0800 to U+FFFF, inclusive
count = 2;
offset = 0xe0;
} else if ((codePoint & 0xffe00000) == 0) {
// U+10000 to U+10FFFF, inclusive
count = 3;
offset = 0xf0;
}
ret += fromCharCode((codePoint >> (6 * count)) + offset);
while (count > 0) {
var tmp = codePoint >> (6 * (count - 1));
ret += fromCharCode(0x80 | (tmp & 0x3f));
count--;
}
return ret;
}
var byteArr,
byteIdx,
byteCount,
codePoint,
bytesSeen,
bytesNeeded,
lowerBoundary,
upperBoundary;
function decodeCodePoint(safe) {
/* eslint-disable no-constant-condition */
while (true) {
if (byteIdx >= byteCount && bytesNeeded) {
if (safe) return goBack();
throw new Error('Invalid byte index');
}
if (byteIdx === byteCount) return false;
var byte = byteArr[byteIdx];
byteIdx++;
if (!bytesNeeded) {
// 0x00 to 0x7F
if ((byte & 0x80) === 0) {
return byte;
} // 0xC2 to 0xDF
if ((byte & 0xe0) === 0xc0) {
bytesNeeded = 1;
codePoint = byte & 0x1f;
} else if ((byte & 0xf0) === 0xe0) {
// 0xE0 to 0xEF
if (byte === 0xe0) lowerBoundary = 0xa0;
if (byte === 0xed) upperBoundary = 0x9f;
bytesNeeded = 2;
codePoint = byte & 0xf;
} else if ((byte & 0xf8) === 0xf0) {
// 0xF0 to 0xF4
if (byte === 0xf0) lowerBoundary = 0x90;
if (byte === 0xf4) upperBoundary = 0x8f;
bytesNeeded = 3;
codePoint = byte & 0x7;
} else {
if (safe) return goBack();
throw new Error('Invalid UTF-8 detected');
}
continue;
}
if (byte < lowerBoundary || byte > upperBoundary) {
if (safe) {
byteIdx--;
return goBack();
}
throw new Error('Invalid continuation byte');
}
lowerBoundary = 0x80;
upperBoundary = 0xbf;
codePoint = (codePoint << 6) | (byte & 0x3f);
bytesSeen++;
if (bytesSeen !== bytesNeeded) continue;
var tmp = codePoint;
codePoint = 0;
bytesNeeded = 0;
bytesSeen = 0;
return tmp;
}
}
function goBack() {
var start = byteIdx - bytesSeen - 1;
byteIdx = start + 1;
codePoint = 0;
bytesNeeded = 0;
bytesSeen = 0;
lowerBoundary = 0x80;
upperBoundary = 0xbf;
return byteArr[start];
}
return exports;
})({});
/* ------------------------------ detectOs ------------------------------ */
export var detectOs = _.detectOs = (function (exports) {
@@ -4638,192 +4373,6 @@ export var map = _.map = (function (exports) {
return exports;
})({});
/* ------------------------------ decodeUriComponent ------------------------------ */
export var decodeUriComponent = _.decodeUriComponent = (function (exports) {
/* Better decodeURIComponent that does not throw if input is invalid.
*
* |Name |Desc |
* |------|----------------|
* |str |String to decode|
* |return|Decoded string |
*/
/* example
* decodeUriComponent('%%25%'); // -> '%%%'
* decodeUriComponent('%E0%A4%A'); // -> '\xE0\xA4%A'
*/
/* typescript
* export declare function decodeUriComponent(str: string): string;
*/
/* dependencies
* each ucs2 map utf8
*/
exports = function(str) {
try {
return decodeURIComponent(str);
} catch (e) {
var matches = str.match(regMatcher);
if (!matches) {
return str;
}
each(matches, function(match) {
str = str.replace(match, decode(match));
});
return str;
}
};
function decode(str) {
str = str.split('%').slice(1);
var bytes = map(str, hexToInt);
str = ucs2.encode(bytes);
str = utf8.decode(str, true);
return str;
}
function hexToInt(numStr) {
return +('0x' + numStr);
}
var regMatcher = /(%[a-f0-9]{2})+/gi;
return exports;
})({});
/* ------------------------------ cookie ------------------------------ */
export var cookie = _.cookie = (function (exports) {
/* Simple api for handling browser cookies.
*
* ### get
*
* Get cookie value.
*
* |Name |Desc |
* |------|--------------------------|
* |key |Cookie key |
* |return|Corresponding cookie value|
*
* ### set
*
* Set cookie value.
*
* |Name |Desc |
* |-------|--------------|
* |key |Cookie key |
* |val |Cookie value |
* |options|Cookie options|
* |return |Module cookie |
*
* ### remove
*
* Remove cookie value.
*
* |Name |Desc |
* |-------|--------------|
* |key |Cookie key |
* |options|Cookie options|
* |return |Module cookie |
*/
/* example
* cookie.set('a', '1', { path: '/' });
* cookie.get('a'); // -> '1'
* cookie.remove('a');
*/
/* typescript
* export declare namespace cookie {
* interface IOptions {
* path?: string;
* expires?: number;
* domain?: string;
* secure?: boolean;
* }
* interface ICookie {
* get(key: string, options?: cookie.IOptions): string;
* set(key: string, val: string, options?: cookie.IOptions): ICookie;
* remove(key: string, options?: cookie.IOptions): ICookie;
* }
* }
* export declare const cookie: cookie.ICookie;
*/
/* dependencies
* defaults isNum isUndef decodeUriComponent
*/
var defOpts = {
path: '/'
};
function setCookie(key, val, options) {
if (!isUndef(val)) {
options = options || {};
options = defaults(options, defOpts);
if (isNum(options.expires)) {
var expires = new Date();
expires.setMilliseconds(
expires.getMilliseconds() + options.expires * 864e5
);
options.expires = expires;
}
val = encodeURIComponent(val);
key = encodeURIComponent(key);
document.cookie = [
key,
'=',
val,
options.expires && '; expires=' + options.expires.toUTCString(),
options.path && '; path=' + options.path,
options.domain && '; domain=' + options.domain,
options.secure ? '; secure' : ''
].join('');
return exports;
}
var cookies = document.cookie ? document.cookie.split('; ') : [];
var result = key ? undefined : {};
for (var i = 0, len = cookies.length; i < len; i++) {
var c = cookies[i];
var parts = c.split('=');
var name = decodeUriComponent(parts.shift());
c = parts.join('=');
c = decodeUriComponent(c);
if (key === name) {
result = c;
break;
}
if (!key) result[name] = c;
}
return result;
}
exports = {
get: setCookie,
set: setCookie,
remove: function(key, options) {
options = options || {};
options.expires = -1;
return setCookie(key, '', options);
}
};
return exports;
})({});
/* ------------------------------ toArr ------------------------------ */
export var toArr = _.toArr = (function (exports) {
@@ -6413,6 +5962,14 @@ export var Emitter = _.Emitter = (function (exports) {
* |event |Event name |
* |...args|Arguments passed to listener|
*
* ### removeAllListeners
*
* Remove all listeners.
*
* |Name |Desc |
* |-----|----------|
* |event|Event name|
*
* ### mixin
*
* [static] Mixin object class methods.
@@ -6437,6 +5994,7 @@ export var Emitter = _.Emitter = (function (exports) {
* off(event: string, listener: types.AnyFn): Emitter;
* once(event: string, listener: types.AnyFn): Emitter;
* emit(event: string, ...args: any[]): Emitter;
* removeAllListeners(event?: string): Emitter;
* static mixin(obj: any): any;
* }
*/
@@ -6481,6 +6039,15 @@ export var Emitter = _.Emitter = (function (exports) {
},
this
);
return this;
},
removeAllListeners: function(event) {
if (!event) {
this._events = {};
} else {
delete this._events[event];
}
return this;
}
},
@@ -7484,93 +7051,6 @@ export var raf = _.raf = (function (exports) {
return exports;
})({});
/* ------------------------------ rmCookie ------------------------------ */
export var rmCookie = _.rmCookie = (function (exports) {
/* Loop through all possible path and domain to remove cookie.
*
* |Name|Desc |
* |----|----------|
* |key |Cookie key|
*/
/* example
* rmCookie('test');
*/
/* typescript
* export declare function rmCookie(key: string): void;
*/
/* dependencies
* cookie
*/
exports = function(key) {
var location = window.location;
var hostname = location.hostname;
var pathname = location.pathname;
var hostNames = hostname.split('.');
var pathNames = pathname.split('/');
var domain = '';
var pathLen = pathNames.length;
var path;
if (del()) return;
for (var i = hostNames.length - 1; i >= 0; i--) {
var hostName = hostNames[i];
if (hostName === '') continue;
domain = domain === '' ? hostName : hostName + '.' + domain;
path = '/';
if (
del({
domain: domain,
path: path
}) ||
del({
domain: domain
})
)
return;
for (var j = 0; j < pathLen; j++) {
var pathName = pathNames[j];
if (pathName === '') continue;
path += pathName;
if (
del({
domain: domain,
path: path
}) ||
del({
path: path
})
)
return;
path += '/';
if (
del({
domain: domain,
path: path
}) ||
del({
path: path
})
)
return;
}
}
function del(options) {
options = options || {};
cookie.remove(key, options);
return !cookie.get(key);
}
};
return exports;
})({});
/* ------------------------------ rtrim ------------------------------ */
export var rtrim = _.rtrim = (function (exports) {