refactor: use licia sameOrigin

This commit is contained in:
surunzi
2020-09-28 12:12:55 +08:00
parent eb4661aae5
commit eb945c03cf
5 changed files with 155 additions and 66 deletions

View File

@@ -1524,10 +1524,6 @@ Check if value is a buffer.
isBuffer(new Buffer(4)); // -> true
```
## isCrossOrig
Check if a url is cross origin.
## isDarkMode
Detect dark mode.
@@ -2031,6 +2027,22 @@ const fibonacci = memoize(function(n) {
});
```
## mergeArr
Merge the contents of arrays together into the first array.
|Name |Desc |
|------|------------------------------------|
|first |Array to merge |
|arrays|Arrays to merge into the first array|
|return|First array |
```javascript
const a = [1, 2];
mergeArr(a, [3, 4], [5, 6]);
console.log(a); // -> [1, 2, 3, 4, 5, 6]
```
## meta
Document meta manipulation, turn name and content into key value pairs.
@@ -2413,6 +2425,24 @@ safeGet(obj, 'a.b'); // -> undefined
Safe localStorage and sessionStorage.
## sameOrigin
Check if two urls pass the same origin policy.
|Name |Desc |
|------|------------------------------------|
|url1 |Url to check |
|url2 |Url to check |
|return|True if urls pass same origin policy|
```javascript
const url1 = 'http://example.com/a.html';
const url2 = 'http://example.com/b.html';
const url3 = 'http://licia.liriliri.io';
sameOrigin(url1, url2); // -> true
sameOrigin(url1, url3); // -> false
```
## slice
Create slice of source array or array-like object.

2
fione

Submodule fione updated: 94408eb8b1...d84e3c467b

View File

@@ -45,7 +45,7 @@
"draggabilly": "^2.2.0",
"eslint": "^6.8.0",
"eslint-loader": "^3.0.3",
"eustia-module": "^1.23.0",
"eustia-module": "^1.26.0",
"handlebars": "^4.7.3",
"handlebars-loader": "^1.7.1",
"html-minifier": "^4.0.0",

View File

@@ -9,7 +9,7 @@ import {
startWith,
trim,
orientation,
isCrossOrig,
sameOrigin,
ajax,
MutationObserver,
toArr,
@@ -341,7 +341,7 @@ export default class Resources extends Tool {
const url = $(this).attr('href')
if (type === 'iframe' || isCrossOrig(url)) {
if (type === 'iframe' || !sameOrigin(location.href, url)) {
showSources('iframe', url)
} else {
ajax({

View File

@@ -856,6 +856,53 @@ export var restArgs = _.restArgs = (function (exports) {
return exports;
})({});
/* ------------------------------ mergeArr ------------------------------ */
export var mergeArr = _.mergeArr = (function (exports) {
/* Merge the contents of arrays together into the first array.
*
* |Name |Desc |
* |------|------------------------------------|
* |first |Array to merge |
* |arrays|Arrays to merge into the first array|
* |return|First array |
*/
/* example
* const a = [1, 2];
* mergeArr(a, [3, 4], [5, 6]);
* console.log(a); // -> [1, 2, 3, 4, 5, 6]
*/
/* typescript
* export declare function mergeArr<T, U>(
* first: ArrayLike<T>,
* ...arrays: ArrayLike<U>[]
* ): ArrayLike<T | U>;
*/
/* dependencies
* restArgs
*/
exports = restArgs(function(first, arrays) {
var end = first.length;
for (var i = 0, len = arrays.length; i < len; i++) {
var arr = arrays[i];
for (var j = 0, _len = arr.length; j < _len; j++) {
first[end++] = arr[j];
}
}
first.length = end;
return first;
});
return exports;
})({});
/* ------------------------------ optimizeCb ------------------------------ */
export var optimizeCb = _.optimizeCb = (function (exports) {
@@ -1895,51 +1942,6 @@ export var isBuffer = _.isBuffer = (function (exports) {
return exports;
})({});
/* ------------------------------ startWith ------------------------------ */
export var startWith = _.startWith = (function (exports) {
/* Check if string starts with the given target string.
*
* |Name |Desc |
* |------|---------------------------------|
* |str |String to search |
* |prefix|String prefix |
* |return|True if string starts with prefix|
*/
/* example
* startWith('ab', 'a'); // -> true
*/
/* typescript
* export declare function startWith(str: string, prefix: string): boolean;
*/
exports = function(str, prefix) {
return str.indexOf(prefix) === 0;
};
return exports;
})({});
/* ------------------------------ isCrossOrig ------------------------------ */
export var isCrossOrig = _.isCrossOrig = (function (exports) {
/* Check if a url is cross origin.
*/
/* dependencies
* startWith
*/
let origin = window.location.origin
exports = function (url) {
return !startWith(url, origin)
}
return exports;
})({});
/* ------------------------------ isEl ------------------------------ */
export var isEl = _.isEl = (function (exports) {
@@ -4563,7 +4565,7 @@ export var Select = _.Select = (function (exports) {
*/
/* dependencies
* Class isStr each types
* Class isStr each types mergeArr
*/
exports = Class({
@@ -4594,18 +4596,6 @@ export var Select = _.Select = (function (exports) {
});
var rootSelect = new exports(document);
function mergeArr(first, second) {
var len = second.length;
var i = first.length;
for (var j = 0; j < len; j++) {
first[i++] = second[j];
}
first.length = i;
return first;
}
return exports;
})({});
@@ -7633,6 +7623,49 @@ export var ajax = _.ajax = (function (exports) {
return exports;
})({});
/* ------------------------------ sameOrigin ------------------------------ */
export var sameOrigin = _.sameOrigin = (function (exports) {
/* Check if two urls pass the same origin policy.
*
* |Name |Desc |
* |------|------------------------------------|
* |url1 |Url to check |
* |url2 |Url to check |
* |return|True if urls pass same origin policy|
*/
/* example
* const url1 = 'http://example.com/a.html';
* const url2 = 'http://example.com/b.html';
* const url3 = 'http://licia.liriliri.io';
* sameOrigin(url1, url2); // -> true
* sameOrigin(url1, url3); // -> false
*/
/* typescript
* export declare function sameOrigin(url1: string, url2: string): boolean;
*/
/* dependencies
* Url
*/
exports = function(url1, url2) {
url1 = new Url(url1);
url2 = new Url(url2);
url1.port = url1.port | 0 || (url1.protocol === 'https' ? 443 : 80);
url2.port = url2.port | 0 || (url2.protocol === 'https' ? 443 : 80);
return (
url1.protocol === url2.protocol &&
url1.hostname === url2.hostname &&
url1.port === url2.port
);
};
return exports;
})({});
/* ------------------------------ sortKeys ------------------------------ */
export var sortKeys = _.sortKeys = (function (exports) {
@@ -7740,6 +7773,32 @@ export var sortKeys = _.sortKeys = (function (exports) {
return exports;
})({});
/* ------------------------------ startWith ------------------------------ */
export var startWith = _.startWith = (function (exports) {
/* Check if string starts with the given target string.
*
* |Name |Desc |
* |------|---------------------------------|
* |str |String to search |
* |prefix|String prefix |
* |return|True if string starts with prefix|
*/
/* example
* startWith('ab', 'a'); // -> true
*/
/* typescript
* export declare function startWith(str: string, prefix: string): boolean;
*/
exports = function(str, prefix) {
return str.indexOf(prefix) === 0;
};
return exports;
})({});
/* ------------------------------ type ------------------------------ */
export var type = _.type = (function (exports) {