// 方案1,IFrame
const iframe = document.createElement('iframe');
iframe.onload = async () => {
console.log(iframe.contentWindow.document.title);
};
iframe.src = 'http://www.*****.com/';
iframe.style.top = '-9999em';
iframe.style.left = '-9999em';
iframe.style.visibility = 'hidden';
document.body.appendChild(iframe);
// 方案2,TextDecoder
const CHARTSET_RE = /(?:charset|encoding)\s{0,10}=\s{0,10}['"]? {0,10}([\w\-]{1,100})/i;
function charset(headers: Headers, html: string) {
let matches: RegExpExecArray = null;
// 先从header里面取
const contentType = headers.get('content-type') ?? headers.get('Content-Type');
if (contentType) {
matches = CHARTSET_RE.exec(contentType);
}
// 再从html里面取
if (!matches) {
matches = CHARTSET_RE.exec(html);
}
// 返回结构
let cs = '';
if (matches) {
cs = matches[1].toLowerCase();
if (cs === 'utf-8') {
cs = 'utf8';
}
}
return cs;
}
fetch('http://www.****.com/').then(async response => {
const buffer = await response.arrayBuffer();
const utf8Decoder = new TextDecoder();
const html = utf8Decoder.decode(buffer);
const cs = charset(response.headers, html);
const decoder = new TextDecoder(cs);
const content = decoder.decode(buffer);
const match = content.match(/<title>([\s\S]+?)<\/title>/ig);
if (match) {
const title = RegExp.$1;
console.log(title);
}
});