@buession/prototype
Version:
A native object extension framework for Javascript.
134 lines (133 loc) • 5.6 kB
JavaScript
/**
* Document 对象扩展
*/
var SameSite;
(function (SameSite) {
SameSite["NONE"] = "None";
SameSite["LAX"] = "Lax";
SameSite["STRICT"] = "Strict";
})(SameSite || (SameSite = {}));
class CookieInstance {
constructor() {}
set(name, value, options) {
var $name = name = encodeURIComponent(name).replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent);
var $value = value ? encodeURIComponent(value).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g, decodeURIComponent) : "";
var stringifiedAttributes = "";
if (options) {
stringifiedAttributes += options.domain ? "; domain=" + options.domain : "";
stringifiedAttributes += options.path ? "; path=" + options.path : "";
if (options.expires) {
var $expiresDate = options.expires instanceof Date ? options.expires : new Date(Date.now() + options.expires * 864e5);
stringifiedAttributes += "; expires=" + $expiresDate.toUTCString();
}
if (Object.isUndefinedOrNull(options.sameSite) === false) {
stringifiedAttributes += "; sameSite=" + options.sameSite;
}
if (Object.isBoolean(options.secure) && options.secure === true) {
stringifiedAttributes += "; secure";
}
if (Object.isBoolean(options.httpOnly) && options.httpOnly === true) {
stringifiedAttributes += "; httpOnly";
}
}
return document.cookie = $name + "=" + $value + stringifiedAttributes;
}
get(name) {
var cookies = document.cookie ? document.cookie.split("; ") : [];
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split("=");
var $name = decodeURIComponent(parts[0]);
var $value = parts.slice(1).join("=");
if ($name === name) {
if ($value[0] === '"') {
$value = $value.slice(1, -1);
}
return $value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
}
}
return null;
}
delete(name, options) {
var $options = options ? options : {};
$options.expires = -1;
this.set(name, "", $options);
}
}
/**
* 检测当前浏览器是否为全屏
*
* @return 当前浏览器是否为全屏
*/
Object.defineProperty(document, "fullScreen", {
value: Object.isUndefined(document.fullscreen) === false ? document.fullscreen : Object.isUndefined(document.mozFullScreen) === false ? document.mozFullScreen : Object.isUndefined(document.webkitIsFullScreen) === false ? document.webkitIsFullScreen : Object.isUndefined(document.msFullScreen) === false ? document.msFullScreen : Object.isUndefined(document.fullscreenElement) === false ? document.fullscreenElement !== null : Object.isUndefined(document.mozFullScreenElement) === false ? document.mozFullScreenElement !== null : Object.isUndefined(document.webkitFullscreenElement) === false ? document.webkitFullscreenElement !== null : Object.isUndefined(document.msFullscreenElement) === false ? document.msFullscreenElement !== null : false,
configurable: true,
writable: false
});
/**
* 检测当前浏览器是否支持全屏模式
*
* @return 当前浏览器是否支持全屏模式
*/
Object.defineProperty(document, "fullScreenEnabled", {
value: Object.isUndefined(document.mozFullScreenEnabled) === false ? document.mozFullScreenEnabled : Object.isUndefined(document.webkitFullscreenEnabled) === false ? document.webkitFullscreenEnabled : Object.isUndefined(document.msFullscreenEnabled) === false ? document.msFullscreenEnabled : Object.isUndefined(document.fullscreenEnabled) === false ? document.fullscreenEnabled : false,
configurable: true,
writable: false
});
/**
* 返回当前文档中正在以全屏模式显示的 Element 节点
*
* @return 当前文档中正在以全屏模式显示的 Element 节点
*/
Object.defineProperty(document, "fullScreenElement", {
value: Object.isUndefined(document.mozFullScreenElement) === false ? document.mozFullScreenElement : Object.isUndefined(document.webkitFullscreenElement) === false ? document.webkitFullscreenElement : Object.isUndefined(document.msFullscreenElement) === false ? document.msFullscreenElement : Object.isUndefined(document.fullscreenElement) === false ? document.fullscreenElement : null,
configurable: true,
writable: false
});
/**
* 返回 Cookie 对象
*
* @return Cookie 对象
*/
Object.defineProperty(document, "httpCookie", {
value: new CookieInstance(),
configurable: true,
writable: false
});
/**
* 请求进入全屏模式
*
* @return Promise
*/
Document.prototype.requestFullscreen = function () {
var doc = document.documentElement;
if (Object.isFunction(doc.mozRequestFullScreen)) {
return doc.mozRequestFullScreen();
} else if (Object.isFunction(doc.webkitRequestFullscreen)) {
return doc.webkitRequestFullscreen();
} else if (Object.isFunction(doc.msRequestFullscreen)) {
return doc.msRequestFullscreen();
} else {
return doc.requestFullscreen();
}
};
/**
* 退出全屏模式
*
* @return Promise
*/
Document.prototype.exitFullscreen = function () {
if (Object.isFunction(document.mozCancelFullScreen)) {
return document.mozCancelFullScreen();
} else if (Object.isFunction(document.mozExitFullScreen)) {
return document.mozExitFullScreen();
} else if (Object.isFunction(document.webkitCancelFullScreen)) {
return document.webkitCancelFullScreen();
} else if (Object.isFunction(document.webkitExitFullscreen)) {
return document.webkitExitFullscreen();
} else if (Object.isFunction(document.msExitFullscreen)) {
return document.msExitFullscreen();
} else {
return document.exitFullscreen();
}
};
;