mirror of https://github.com/qist/tvbox.git
parent
828cfd67fe
commit
3a9658cbb6
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
|||||||
0781ce50eb420e06572003b938aac220
|
386350685603251deb582749f2a79360
|
||||||
File diff suppressed because one or more lines are too long
@ -1 +1 @@
|
|||||||
7cdb41acf1d6e9f50c551a80c8c60ed5
|
880995fd4c5d4db2a34315a562333cf8
|
||||||
@ -0,0 +1,328 @@
|
|||||||
|
/*
|
||||||
|
* @File : lovemovie.js
|
||||||
|
* @Author : jade
|
||||||
|
* @Date : 2024/4/29 09:36
|
||||||
|
* @Email : jadehh@1ive.com
|
||||||
|
* @Software : Samples
|
||||||
|
* @Desc : 爱情电影网
|
||||||
|
*/
|
||||||
|
import {Spider} from "./spider.js";
|
||||||
|
import {_, Crypto, load} from "../lib/cat.js";
|
||||||
|
import {VodDetail, VodShort} from "../lib/vod.js";
|
||||||
|
import * as Utils from "../lib/utils.js";
|
||||||
|
import {gbk_us} from "../lib/gbk_us.js";
|
||||||
|
|
||||||
|
class LoveMovieSpider extends Spider {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.siteUrl = "https://b.aqdyje.com"
|
||||||
|
this.removeKey = "骑兵营"
|
||||||
|
}
|
||||||
|
|
||||||
|
getName() {
|
||||||
|
return "💕┃爱情电影网┃💕"
|
||||||
|
}
|
||||||
|
|
||||||
|
getAppName() {
|
||||||
|
return "爱情电影网"
|
||||||
|
}
|
||||||
|
|
||||||
|
getJSName() {
|
||||||
|
return "lovemovie"
|
||||||
|
}
|
||||||
|
|
||||||
|
getType() {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
|
||||||
|
async init(cfg) {
|
||||||
|
await super.init(cfg);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getHtml(url = this.siteUrl, proxy = false, headers = this.getHeader()) {
|
||||||
|
let buffer = await this.fetch(url, null, headers, false, false, 1, proxy)
|
||||||
|
|
||||||
|
let html = Utils.decode(buffer, "gb2312")
|
||||||
|
await this.jadeLog.debug(`html content:${html}`)
|
||||||
|
if (!_.isEmpty(html)) {
|
||||||
|
return load(html)
|
||||||
|
} else {
|
||||||
|
return load(gbkDecode(buffer))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async getFilter($, navElements) {
|
||||||
|
let extend_list = []
|
||||||
|
let extend_dic = {"key": "class", "name": "类型", "value": [this.getFliterDic("全部", "全部")]}
|
||||||
|
for (const navElement of $(navElements).find("li")) {
|
||||||
|
let element = $(navElement).find("a")[0]
|
||||||
|
let type_name = $(element).text()
|
||||||
|
let type_id = element.attribs.href
|
||||||
|
extend_dic["value"].push(this.getFliterDic(type_name, type_id))
|
||||||
|
}
|
||||||
|
if (extend_dic["value"].length > 1) {
|
||||||
|
extend_list.push(extend_dic)
|
||||||
|
}
|
||||||
|
return extend_list
|
||||||
|
}
|
||||||
|
|
||||||
|
async setClasses() {
|
||||||
|
let $ = await this.getHtml()
|
||||||
|
let navElements = $("[class=\"nav-item drop-down \"]")
|
||||||
|
for (const navElement of navElements) {
|
||||||
|
let elemenet = $(navElement).find("a")[0]
|
||||||
|
let type_name = $(elemenet).text()
|
||||||
|
let type_id = elemenet.attribs.href
|
||||||
|
if (type_name !== this.removeKey) {
|
||||||
|
this.classes.push(this.getTypeDic(type_name, type_id))
|
||||||
|
this.filterObj[type_id] = await this.getFilter($, navElement)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async parseVodShortListFromDoc($) {
|
||||||
|
let vod_list = []
|
||||||
|
let vodElements = $("[class=\"play-img\"]")
|
||||||
|
if (vodElements.length === 0) {
|
||||||
|
vodElements = $("[class=\"show-list list-mode fn-clear\"]").find("li")
|
||||||
|
}
|
||||||
|
for (const vodElement of vodElements) {
|
||||||
|
let vodShort = new VodShort()
|
||||||
|
vodShort.vod_id = vodElement.attribs.href
|
||||||
|
if (_.isEmpty(vodShort.vod_id)) {
|
||||||
|
vodShort.vod_id = $(vodElement).find("a")[0].attribs.href
|
||||||
|
}
|
||||||
|
let imgElement = $(vodElement).find("img")[0]
|
||||||
|
vodShort.vod_pic = imgElement.attribs.src
|
||||||
|
vodShort.vod_name = imgElement.attribs.alt
|
||||||
|
for (const element of $(vodElement).find("label")) {
|
||||||
|
let text = $(element).text().trim()
|
||||||
|
if (!_.isEmpty(text)) {
|
||||||
|
vodShort.vod_remarks = text
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (_.isEmpty(vodShort.vod_remarks)) {
|
||||||
|
vodShort.vod_remarks = $($(vodElement).find("p")[0]).text().replace("\n")
|
||||||
|
}
|
||||||
|
vod_list.push(vodShort)
|
||||||
|
}
|
||||||
|
return vod_list
|
||||||
|
}
|
||||||
|
|
||||||
|
async parseVodShortListFromDocBySearch($) {
|
||||||
|
let vodElements = $("[class=\"show-list\"]").find("li")
|
||||||
|
let vod_list = []
|
||||||
|
for (const vodElement of vodElements) {
|
||||||
|
let vodShort = new VodShort()
|
||||||
|
vodShort.vod_id = $(vodElement).find("a")[0].attribs.href
|
||||||
|
let imgElement = $(vodElement).find("img")[0]
|
||||||
|
vodShort.vod_pic = imgElement.attribs.src
|
||||||
|
vodShort.vod_name = imgElement.attribs.alt
|
||||||
|
vodShort.vod_remarks = $($(vodElement).find("[class=\"type fn-left\"]")).text().replace("类型:", "")
|
||||||
|
if (vodShort.vod_remarks !== "社处片" && vodShort.vod_remarks !== "社保片" && vodShort.vod_remarks !== "撸丝片" && vodShort.vod_remarks !== "撸丝动漫") {
|
||||||
|
vod_list.push(vodShort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vod_list
|
||||||
|
}
|
||||||
|
|
||||||
|
async parseVodDetailFromDoc($) {
|
||||||
|
let vodDetail = new VodDetail()
|
||||||
|
let imgElement = $("[class=\"detail-pic fn-left\"]").find("img")[0]
|
||||||
|
vodDetail.vod_pic = imgElement.attribs.src
|
||||||
|
vodDetail.vod_name = imgElement.attribs.alt
|
||||||
|
let vodInfoElement = $("[class=\"info fn-clear\"]")
|
||||||
|
for (const vodDlElement of $(vodInfoElement).find("dl")) {
|
||||||
|
let text = $(vodDlElement).text()
|
||||||
|
if (text.indexOf("主演") > -1) {
|
||||||
|
vodDetail.vod_actor = text.replaceAll("主演:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
if (text.indexOf("状态") > -1) {
|
||||||
|
vodDetail.vod_remarks = text.replaceAll("状态:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
if (text.indexOf("类型") > -1) {
|
||||||
|
vodDetail.type_name = text.replaceAll("类型:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
if (text.indexOf("地区") > -1) {
|
||||||
|
vodDetail.vod_area = text.replaceAll("地区:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
if (text.indexOf("导演") > -1) {
|
||||||
|
vodDetail.vod_director = text.replaceAll("导演:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
if (text.indexOf("年份") > -1) {
|
||||||
|
vodDetail.vod_year = text.replaceAll("年份:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
if (text.indexOf("剧情") > -1) {
|
||||||
|
vodDetail.vod_content = text.replaceAll("剧情:", "").replaceAll("\n", "")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let playList = {}
|
||||||
|
let html = $.html()
|
||||||
|
let playListElements = $("[class=\"play-list\"]")
|
||||||
|
let index = 1
|
||||||
|
for (const playListElement of playListElements) {
|
||||||
|
let playName = `播放连接-${index}`
|
||||||
|
let vodItems = []
|
||||||
|
for (const playUrlElement of $(playListElement).find("a")) {
|
||||||
|
let playUrlName = playUrlElement.attribs.title
|
||||||
|
let playUrl = playUrlElement.attribs.href
|
||||||
|
vodItems.push(playUrlName + "$" + playUrl)
|
||||||
|
}
|
||||||
|
playList[playName] = vodItems.join("#")
|
||||||
|
index = index + 1
|
||||||
|
}
|
||||||
|
index = 1
|
||||||
|
let ciliListElements = $("[class=\"con4\"]")
|
||||||
|
for (const ciliListElement of ciliListElements) {
|
||||||
|
let playName = `磁力链接-${index}`
|
||||||
|
let vodItems = []
|
||||||
|
let playUrlName = playName
|
||||||
|
let playUrl = $($(ciliListElement).find("div")).find("a")[0].attribs.href
|
||||||
|
if (playUrl !== "javascript:void(0);") {
|
||||||
|
vodItems.push(playUrlName + "$" + playUrl)
|
||||||
|
playList[playName] = vodItems.join("#")
|
||||||
|
index = index + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
vodDetail.vod_play_url = _.values(playList).join('$$$');
|
||||||
|
vodDetail.vod_play_from = _.keys(playList).join('$$$');
|
||||||
|
return vodDetail
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async setHomeVod() {
|
||||||
|
let $ = await this.getHtml()
|
||||||
|
this.homeVodList = await this.parseVodShortListFromDoc($)
|
||||||
|
}
|
||||||
|
|
||||||
|
async setDetail(id) {
|
||||||
|
let $ = await this.getHtml(this.siteUrl + id)
|
||||||
|
this.vodDetail = await this.parseVodDetailFromDoc($)
|
||||||
|
}
|
||||||
|
|
||||||
|
getExtend(extend) {
|
||||||
|
if (extend["class"] !== undefined) {
|
||||||
|
if (extend["class"] !== "全部") {
|
||||||
|
return extend["class"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
async setCategory(tid, pg, filter, extend) {
|
||||||
|
let classes = this.getExtend(extend) ?? tid
|
||||||
|
let url
|
||||||
|
if (classes === tid) {
|
||||||
|
url = this.siteUrl + classes
|
||||||
|
} else {
|
||||||
|
if (parseInt(pg) === 1) {
|
||||||
|
url = this.siteUrl + classes
|
||||||
|
} else {
|
||||||
|
url = this.siteUrl + classes + `index${pg}.html`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let $ = await this.getHtml(url)
|
||||||
|
this.vodList = await this.parseVodShortListFromDoc($)
|
||||||
|
}
|
||||||
|
|
||||||
|
async setPlay(flag, id, flags) {
|
||||||
|
if (flag.indexOf("磁力") > -1) {
|
||||||
|
this.playUrl = id
|
||||||
|
} else {
|
||||||
|
let idSplitList = id.split("-")
|
||||||
|
let flag_id = parseInt(idSplitList[1])
|
||||||
|
let episode = parseInt(idSplitList[2].split(".")[0])
|
||||||
|
let $ = await this.getHtml(this.siteUrl + id)
|
||||||
|
let playJsUrl = Utils.getStrByRegex(/<script type="text\/javascript" src="(.*?)">/, $.html())
|
||||||
|
let playJsContent = await this.fetch(this.siteUrl + playJsUrl, null, this.getHeader())
|
||||||
|
let playUrlListStr = Utils.getStrByRegex(/var VideoListJson=(.*?),urlinfo=/, playJsContent)
|
||||||
|
let playDic = eval(playUrlListStr)
|
||||||
|
this.playUrl = playDic[flag_id][1][episode].split("$")[1]
|
||||||
|
if (this.playUrl.indexOf("m3u8") === -1) {
|
||||||
|
let html = await this.fetch(this.playUrl, null, this.getHeader())
|
||||||
|
this.playUrl = Utils.getStrByRegex(/url: '(.*?)'/, html)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GBKEncode(str) {
|
||||||
|
var arr_index = 0x8140; //33088;
|
||||||
|
str += '';
|
||||||
|
var gbk = [];
|
||||||
|
var wh = '?'.charCodeAt(0); //gbk中没有的字符的替换符
|
||||||
|
for (var i = 0; i < str.length; i++) {
|
||||||
|
var charcode = str.charCodeAt(i);
|
||||||
|
if (charcode < 0x80) gbk.push(charcode);
|
||||||
|
else {
|
||||||
|
var gcode = gbk_us.indexOf(charcode);
|
||||||
|
if (~gcode) {
|
||||||
|
gcode += arr_index;
|
||||||
|
gbk.push(0xFF & (gcode >> 8), 0xFF & gcode);
|
||||||
|
} else {
|
||||||
|
gbk.push(wh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return gbk;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
encode(str) {
|
||||||
|
let encodeStr = ""
|
||||||
|
for (const ch of str) {
|
||||||
|
let bitArr = this.GBKEncode(ch);
|
||||||
|
for (let i = 0; i < bitArr.length; i++) {
|
||||||
|
bitArr[i] = '%' + ('0' + bitArr[i].toString(16)).substr(-2).toUpperCase();
|
||||||
|
}
|
||||||
|
encodeStr = encodeStr + bitArr.join('');
|
||||||
|
}
|
||||||
|
return encodeStr
|
||||||
|
}
|
||||||
|
|
||||||
|
async setSearch(wd, quick) {
|
||||||
|
let params = {"searchword": this.encode(wd)}
|
||||||
|
let buffer = await this.post(this.siteUrl + "/search.asp", params, this.getHeader(), "form", 1)
|
||||||
|
let $ = load(Utils.decode(buffer, "gb2312"))
|
||||||
|
this.vodList = await this.parseVodShortListFromDocBySearch($)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
let spider = new LoveMovieSpider()
|
||||||
|
|
||||||
|
async function init(cfg) {
|
||||||
|
await spider.init(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function home(filter) {
|
||||||
|
return await spider.home(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function homeVod() {
|
||||||
|
return await spider.homeVod()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function category(tid, pg, filter, extend) {
|
||||||
|
return await spider.category(tid, pg, filter, extend)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function detail(id) {
|
||||||
|
return await spider.detail(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function play(flag, id, flags) {
|
||||||
|
return await spider.play(flag, id, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function search(wd, quick) {
|
||||||
|
return await spider.search(wd, quick)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function __jsEvalReturn() {
|
||||||
|
return {
|
||||||
|
init: init, home: home, homeVod: homeVod, category: category, detail: detail, play: play, search: search,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export {spider, LoveMovieSpider}
|
||||||
@ -0,0 +1,133 @@
|
|||||||
|
/*
|
||||||
|
* @File : lovemovie18.js
|
||||||
|
* @Author : jade
|
||||||
|
* @Date : 2024/4/29 09:36
|
||||||
|
* @Email : jadehh@1ive.com
|
||||||
|
* @Software : Samples
|
||||||
|
* @Desc : 爱情电影网
|
||||||
|
*/
|
||||||
|
import {LoveMovieSpider} from "./lovemovie.js";
|
||||||
|
import {VodShort} from "../lib/vod.js";
|
||||||
|
|
||||||
|
class LoveMovie18Spider extends LoveMovieSpider {
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.siteUrl = "https://b.aqdyje.com"
|
||||||
|
this.removeKey = "骑兵营"
|
||||||
|
}
|
||||||
|
|
||||||
|
getName() {
|
||||||
|
return "🔞┃爱情电影网18+┃🔞"
|
||||||
|
}
|
||||||
|
|
||||||
|
getAppName() {
|
||||||
|
return "爱情电影网18+"
|
||||||
|
}
|
||||||
|
|
||||||
|
getJSName() {
|
||||||
|
return "lovemovie18"
|
||||||
|
}
|
||||||
|
|
||||||
|
getType() {
|
||||||
|
return 3
|
||||||
|
}
|
||||||
|
|
||||||
|
async parseVodShortListFromDocBySearch($) {
|
||||||
|
let vodElements = $("[class=\"show-list\"]").find("li")
|
||||||
|
let vod_list = []
|
||||||
|
for (const vodElement of vodElements) {
|
||||||
|
let vodShort = new VodShort()
|
||||||
|
vodShort.vod_id = $(vodElement).find("a")[0].attribs.href
|
||||||
|
let imgElement = $(vodElement).find("img")[0]
|
||||||
|
vodShort.vod_pic = imgElement.attribs.src
|
||||||
|
vodShort.vod_name = imgElement.attribs.alt
|
||||||
|
vodShort.vod_remarks = $($(vodElement).find("[class=\"type fn-left\"]")).text().replace("类型:", "")
|
||||||
|
if (vodShort.vod_remarks === "社处片" || vodShort.vod_remarks === "社保片" || vodShort.vod_remarks === "撸丝片" || vodShort.vod_remarks === "撸丝动漫") {
|
||||||
|
vod_list.push(vodShort)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return vod_list
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async getFilter(type_id) {
|
||||||
|
let $ = await this.getHtml(this.siteUrl + type_id)
|
||||||
|
let extend_list = []
|
||||||
|
let extend_dic = {"key": "class", "name": "类型", "value": []}
|
||||||
|
for (const navElement of $("[class=\"subnav-tv fn-left\"]").find("a")) {
|
||||||
|
let type_name = $(navElement).text()
|
||||||
|
let type_id = navElement.attribs.href
|
||||||
|
extend_dic["value"].push(this.getFliterDic(type_name, type_id))
|
||||||
|
}
|
||||||
|
if (extend_dic["value"].length > 1) {
|
||||||
|
extend_list.push(extend_dic)
|
||||||
|
}
|
||||||
|
return extend_list
|
||||||
|
}
|
||||||
|
|
||||||
|
async setClasses() {
|
||||||
|
let $ = await this.getHtml()
|
||||||
|
let navElements = $("[class=\"nav-item drop-down \"]")
|
||||||
|
this.classes = []
|
||||||
|
for (const navElement of navElements) {
|
||||||
|
let element = $(navElement).find("a")[0]
|
||||||
|
let type_name = $(element).text()
|
||||||
|
let type_id = element.attribs.href
|
||||||
|
if (type_name === this.removeKey) {
|
||||||
|
this.classes.push(this.getTypeDic(type_name, type_id))
|
||||||
|
this.filterObj[type_id] = await this.getFilter(type_id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async setCategory(tid, pg, filter, extend) {
|
||||||
|
let classes = this.getExtend(extend) ?? tid
|
||||||
|
let url
|
||||||
|
if (parseInt(pg) === 1) {
|
||||||
|
url = this.siteUrl + classes
|
||||||
|
} else {
|
||||||
|
url = this.siteUrl + classes + `index${pg}.html`
|
||||||
|
}
|
||||||
|
let $ = await this.getHtml(url)
|
||||||
|
this.vodList = await this.parseVodShortListFromDoc($)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
let spider = new LoveMovie18Spider()
|
||||||
|
|
||||||
|
async function init(cfg) {
|
||||||
|
await spider.init(cfg)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function home(filter) {
|
||||||
|
return await spider.home(filter)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function homeVod() {
|
||||||
|
return await spider.homeVod()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function category(tid, pg, filter, extend) {
|
||||||
|
return await spider.category(tid, pg, filter, extend)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function detail(id) {
|
||||||
|
return await spider.detail(id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function play(flag, id, flags) {
|
||||||
|
return await spider.play(flag, id, flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
async function search(wd, quick) {
|
||||||
|
return await spider.search(wd, quick)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function __jsEvalReturn() {
|
||||||
|
return {
|
||||||
|
init: init, home: home, homeVod: homeVod, category: category, detail: detail, play: play, search: search,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export {spider}
|
||||||
@ -0,0 +1,107 @@
|
|||||||
|
import Stream, { DEFAULT_ENCODING, getEncoding } from './text_decoder_index.js'
|
||||||
|
import { end_of_stream, finished, stringToCodePoints } from './text_decoder_utils.js'
|
||||||
|
import { encoders } from './table.js'
|
||||||
|
|
||||||
|
// 8.2 Interface TextEncoder
|
||||||
|
|
||||||
|
class TextEncoder {
|
||||||
|
/**
|
||||||
|
* @param {string=} label The label of the encoding. NONSTANDARD.
|
||||||
|
* @param {Object=} [options] NONSTANDARD.
|
||||||
|
*/
|
||||||
|
constructor(label, options = {}) {
|
||||||
|
// A TextEncoder object has an associated encoding and encoder.
|
||||||
|
|
||||||
|
/** @private */
|
||||||
|
this._encoding = null
|
||||||
|
/** @private @type {?Encoder} */
|
||||||
|
this._encoder = null
|
||||||
|
|
||||||
|
// Non-standard
|
||||||
|
/** @private @type {boolean} */
|
||||||
|
this._do_not_flush = false
|
||||||
|
/** @private @type {string} */
|
||||||
|
this._fatal = options['fatal'] ? 'fatal' : 'replacement'
|
||||||
|
|
||||||
|
// 2. Set enc's encoding to UTF-8's encoder.
|
||||||
|
if (options['NONSTANDARD_allowLegacyEncoding']) {
|
||||||
|
// NONSTANDARD behavior.
|
||||||
|
label = label !== undefined ? String(label) : DEFAULT_ENCODING
|
||||||
|
var encoding = getEncoding(label)
|
||||||
|
if (encoding === null || encoding.name === 'replacement')
|
||||||
|
throw RangeError('Unknown encoding: ' + label)
|
||||||
|
if (!encoders[encoding.name]) {
|
||||||
|
throw Error('Encoder not present.' +
|
||||||
|
' Did you forget to include encoding-indexes.js first?')
|
||||||
|
}
|
||||||
|
this._encoding = encoding
|
||||||
|
} else {
|
||||||
|
// Standard behavior.
|
||||||
|
this._encoding = getEncoding('utf-8')
|
||||||
|
|
||||||
|
if (label !== undefined && 'console' in global) {
|
||||||
|
console.warn('TextEncoder constructor called with encoding label, '
|
||||||
|
+ 'which is ignored.')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
get encoding() {
|
||||||
|
return this._encoding.name.toLowerCase()
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @param {string=} opt_string The string to encode.
|
||||||
|
* @param {Object=} options
|
||||||
|
*/
|
||||||
|
encode(opt_string = '', options = {}) {
|
||||||
|
// NOTE: This option is nonstandard. None of the encodings
|
||||||
|
// permitted for encoding (i.e. UTF-8, UTF-16) are stateful when
|
||||||
|
// the input is a USVString so streaming is not necessary.
|
||||||
|
if (!this._do_not_flush)
|
||||||
|
this._encoder = encoders[this._encoding.name]({
|
||||||
|
fatal: this._fatal === 'fatal' })
|
||||||
|
this._do_not_flush = Boolean(options['stream'])
|
||||||
|
|
||||||
|
// 1. Convert input to a stream.
|
||||||
|
const input = new Stream(stringToCodePoints(opt_string))
|
||||||
|
|
||||||
|
// 2. Let output be a new stream
|
||||||
|
const output = []
|
||||||
|
|
||||||
|
/** @type {?(number|!Array.<number>)} */
|
||||||
|
var result
|
||||||
|
// 3. While true, run these substeps:
|
||||||
|
while (true) {
|
||||||
|
// 1. Let token be the result of reading from input.
|
||||||
|
var token = input.read()
|
||||||
|
if (token === end_of_stream)
|
||||||
|
break
|
||||||
|
// 2. Let result be the result of processing token for encoder,
|
||||||
|
// input, output.
|
||||||
|
result = this._encoder.handler(input, token)
|
||||||
|
if (result === finished)
|
||||||
|
break
|
||||||
|
if (Array.isArray(result))
|
||||||
|
output.push.apply(output, /**@type {!Array.<number>}*/(result))
|
||||||
|
else
|
||||||
|
output.push(result)
|
||||||
|
}
|
||||||
|
// TODO: Align with spec algorithm.
|
||||||
|
if (!this._do_not_flush) {
|
||||||
|
while (true) {
|
||||||
|
result = this._encoder.handler(input, input.read())
|
||||||
|
if (result === finished)
|
||||||
|
break
|
||||||
|
if (Array.isArray(result))
|
||||||
|
output.push.apply(output, /**@type {!Array.<number>}*/(result))
|
||||||
|
else
|
||||||
|
output.push(result)
|
||||||
|
}
|
||||||
|
this._encoder = null
|
||||||
|
}
|
||||||
|
// 3. If result is finished, convert output into a byte sequence,
|
||||||
|
// and then return a Uint8Array object wrapping an ArrayBuffer
|
||||||
|
// containing output.
|
||||||
|
return new Uint8Array(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export {TextEncoder}
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue