refactor: use arrow function

This commit is contained in:
Tony 2026-05-11 02:16:04 +08:00
parent 3b0d76ae20
commit a98de2effd
83 changed files with 290 additions and 287 deletions

View File

@ -360,6 +360,22 @@
{
"selector": "CallExpression[callee.name=\"load\"] AwaitExpression > CallExpression",
"message": "Do not use await in call expressions. Extract the result into a variable first."
},
{
"selector": "Identifier[name=\"_ctx\"]",
"message": "Usage of `_ctx` is not allowed."
},
{
"selector": "CallExpression[callee.property.name=\"each\"] > FunctionExpression",
"message": "Use an arrow function instead."
},
{
"selector": "CallExpression[callee.property.name=\"filter\"] > FunctionExpression",
"message": "Use an arrow function instead."
},
{
"selector": "CallExpression[callee.property.name=\"map\"] > FunctionExpression",
"message": "Use an arrow function instead."
}
],

View File

@ -59,13 +59,13 @@ const replaceUrl = (template?: string, url?: string) => {
};
const replaceUrls = ($: CheerioAPI, selector: string, template: string, attribute = 'src') => {
$(selector).each(function () {
const oldSrc = $(this).attr(attribute);
$(selector).each((_, el) => {
const oldSrc = $(el).attr(attribute);
if (oldSrc) {
const url = parseUrl(oldSrc);
if (url && url.protocol !== 'data:') {
// Cheerio will do the right thing to prohibit XSS.
$(this).attr(attribute, interpolate(template, url));
$(el).attr(attribute, interpolate(template, url));
}
}
});

View File

@ -163,10 +163,10 @@ async function handler(ctx) {
content('.m-linkCard').remove();
content('.m-photo').each(function () {
content(this).html(
content('.m-photo').each((_, el) => {
content(el).html(
renderExclusiveDescription({
image: content(this).find('img').attr('data-src'),
image: content(el).find('img').attr('data-src'),
})
);
});

View File

@ -167,8 +167,8 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('.bot_word, .js-open-app, .s-img').remove();
content('video').each(function () {
content(this).attr('src', content(this).attr('data-src'));
content('video').each((_, el) => {
content(el).attr('src', content(el).attr('data-src'));
});
content('.article-body .image-lazy').each((_, elem) => {
elem.attribs.src = elem.attribs['data-src'] ?? elem.attribs.src;

View File

@ -133,13 +133,13 @@ async function handler(ctx) {
content('.ads, .tips').remove();
content('ignore_js_op').each(function () {
const img = content(this).find('img');
content('ignore_js_op').each((_, el) => {
const img = content(el).find('img');
const originalSrc = img.attr('data-original');
const fallbackSrc = img.attr('src');
// 判断是否有 data-original 属性,若有则使用其值,否则使用 src 属性值
const imgSrc = originalSrc || fallbackSrc;
content(this).replaceWith(`<img src="${imgSrc}">`);
content(el).replaceWith(`<img src="${imgSrc}">`);
});
item.author = content('.fl.black').first().text();
@ -174,8 +174,8 @@ async function handler(ctx) {
}
}
content('.showhide img').each(function () {
readTpc.append(`<br><img style="max-width: 100%;" src="${content(this).attr('src')}">`);
content('.showhide img').each((_, el) => {
readTpc.append(`<br><img style="max-width: 100%;" src="${content(el).attr('src')}">`);
});
item.description = readTpc.html();

View File

@ -129,11 +129,11 @@ async function handler(ctx) {
const pubDate = content('span.pub-time').text() || content('span.fby span').first().prop('title') || content('span.fby').first().text().split('发表于').pop().trim();
content('img').each(function () {
content(this).replaceWith(
content('img').each((_, el) => {
content(el).replaceWith(
renderToString(
<figure>
<img src={content(this).prop('file')} alt={content(this).prop('alt')} />
<img src={content(el).prop('file')} alt={content(el).prop('alt')} />
</figure>
)
);

View File

@ -105,8 +105,8 @@ async function handler(ctx) {
content('#body *, div[data-component="FeatureMedia"]')
.children()
.each(function () {
const element = content(this);
.each((_, el) => {
const element = content(el);
if (element.prop('tagName').toLowerCase() === 'figure') {
element.replaceWith(
renderDescription({

View File

@ -48,8 +48,8 @@ async function handler(ctx) {
$detail('article.post-content').find('.lwptoc').remove();
$detail('article.post-content').find('#related_posts').remove();
$detail('article.post-content').find('.entry-copyright').remove();
$detail('article.post-content img').each(function () {
$detail(this).replaceWith(`<img src=https:${$detail(this).attr('src')}>`);
$detail('article.post-content img').each((_, el) => {
$detail(el).replaceWith(`<img src=https:${$detail(el).attr('src')}>`);
});
item.description = $detail('article.post-content').html();
return item;

View File

@ -53,8 +53,8 @@ async function handler() {
const title = item.attr('data-text');
let description = '';
item.nextUntil('h4').each(function () {
description += $(this).html();
item.nextUntil('h4').each((_, el) => {
description += $(el).html();
});
return {

View File

@ -52,13 +52,13 @@ async function handler(ctx) {
// To handle lazy-loaded images.
content('figure').each(function () {
const image = content(this).find('img');
content('figure').each((_, el) => {
const image = content(el).find('img');
const src = (image.prop('data-actualsrc') ?? image.prop('data-original') ?? image.prop('src')).replace(/(-\d+x\d+)/, '');
const width = image.prop('data-rawwidth') ?? image.prop('width');
const height = image.prop('data-rawheight') ?? image.prop('height');
content(this).replaceWith(
content(el).replaceWith(
renderToString(
<figure>
<img src={src} width={width} height={height} />

View File

@ -33,18 +33,18 @@ async function handler(ctx) {
const a = item.find('a.title');
item.find('img').each(function () {
$(this).attr('src', $(this).attr('data-echo'));
$(this).removeClass('lazy');
$(this).removeAttr('data-echo');
$(this).removeAttr('id');
item.find('img').each((_, el) => {
$(el).attr('src', $(el).attr('data-echo'));
$(el).removeClass('lazy');
$(el).removeAttr('data-echo');
$(el).removeAttr('id');
});
item.find('video').each(function () {
$(this).attr('poster', $(this).attr('data-echo'));
$(this).removeAttr('data-echo');
$(this).removeAttr('onerror');
$(this).removeAttr('id');
item.find('video').each((_, el) => {
$(el).attr('poster', $(el).attr('data-echo'));
$(el).removeAttr('data-echo');
$(el).removeAttr('onerror');
$(el).removeAttr('id');
});
return {

View File

@ -129,13 +129,13 @@ async function handler(ctx) {
content('.attachlist')
.find('a')
.each(function () {
content(this)
.each((_, el) => {
content(el)
.children('img')
.attr('src', `${rootUrl}${content(this).children('img').attr('src')}`);
content(this).attr(
.attr('src', `${rootUrl}${content(el).children('img').attr('src')}`);
content(el).attr(
'href',
`${rootUrl}/${content(this)
`${rootUrl}/${content(el)
.attr('href')
.replace(/^attach-dialog/, 'attach-download')}`
);

View File

@ -98,8 +98,8 @@ async function handler(ctx: Context) {
const newsContent = content('.v_news_content');
// 移除不必要的标签,比如 <p> 和 <span> 中无用的内容
newsContent.find('p, span, strong').each(function () {
const element = content(this);
newsContent.find('p, span, strong').each((_, el) => {
const element = content(el);
const text = element.text().trim();
// 删除没有有用文本的元素,防止空元素被保留

View File

@ -73,8 +73,8 @@ async function handler() {
const content = load(detailResponse.data);
const newsContent = content('.v_news_content');
newsContent.find('p, span, strong').each(function () {
const element = content(this);
newsContent.find('p, span, strong').each((_, el) => {
const element = content(el);
const text = element.text().trim();
if (text === '') {
element.remove();

View File

@ -44,13 +44,13 @@ async function handler(ctx) {
// To handle lazy-loaded images from external sites.
content('figure').each(function () {
const image = content(this).find('img');
content('figure').each((_, el) => {
const image = content(el).find('img');
const src = image.prop('data-actualsrc') ?? image.prop('data-original');
const width = image.prop('data-rawwidth');
const height = image.prop('data-rawheight');
content(this).replaceWith(
content(el).replaceWith(
renderFigure({
src,
width,
@ -61,13 +61,13 @@ async function handler(ctx) {
// To remove watermarks on images.
content('p img').each(function () {
const image = content(this);
content('p img').each((_, el) => {
const image = content(el);
const src = image.prop('src').split('!')[0];
const width = image.prop('width');
const height = image.prop('height');
content(this).replaceWith(
content(el).replaceWith(
renderFigure({
src,
width,

View File

@ -61,9 +61,7 @@ async function handler(ctx) {
item.description = content('div.left_zw').html();
const info = content('div.left-t')
.contents()
.filter(function () {
return this.type === 'text';
})
.filter((_, el) => el.type === 'text')
.text()
.split(' ');
item.pubDate = timezone(parseDate(info[0], 'YYYY年MM月DD日 HH:mm'), +8);

View File

@ -87,9 +87,7 @@ async function handler(ctx) {
}
const filesHTML = renderSource(i);
let $ = load(filesHTML);
const coomerFiles = $('img, a, audio, video').map(function () {
return $(this).prop('outerHTML')!;
});
const coomerFiles = $('img, a, audio, video').map((_, el) => $(el).prop('outerHTML')!);
let desc = '';
if (i.content) {
desc += `<div>${i.content}</div>`;
@ -97,11 +95,11 @@ async function handler(ctx) {
$ = load(desc);
let count = 0;
const regex = /downloads.fanbox.cc/;
$('a').each(function () {
const link = $(this).attr('href');
$('a').each((_, el) => {
const link = $(el).attr('href');
if (regex.test(link!)) {
count++;
$(this).replaceWith(coomerFiles[count]);
$(el).replaceWith(coomerFiles[count]);
}
});
desc = (coomerFiles.length > 0 ? coomerFiles[0] : '') + $.html();
@ -110,8 +108,8 @@ async function handler(ctx) {
}
let enclosureInfo = {};
load(desc)('audio source, video source').each(function () {
const src = $(this).attr('src') ?? '';
load(desc)('audio source, video source').each((_, el) => {
const src = $(el).attr('src') ?? '';
const mimeType =
{
m4a: 'audio/mp4',

View File

@ -89,11 +89,11 @@ const processItems = async (apiUrl, limit, tryGet, ...params) => {
const content = load(data.content);
content('img').each(function () {
if (content(this).prop('src')) {
content(this).prop('src', content(this).prop('src').split('?')[0]);
content('img').each((_, el) => {
if (content(el).prop('src')) {
content(el).prop('src', content(el).prop('src').split('?')[0]);
} else {
content(this).remove();
content(el).remove();
}
});

View File

@ -31,8 +31,8 @@ async function handler() {
$('.cameo').remove();
$('.topvote_title_desc, .topvote_title_minimal, .topvote_minimal').each(function () {
$(this).find('a').first().remove();
$('.topvote_title_desc, .topvote_title_minimal, .topvote_minimal').each((_, el) => {
$(el).find('a').first().remove();
});
let items = $('#article_index a')

View File

@ -63,8 +63,8 @@ async function handler() {
content('.wp-block-spacer').remove();
content('.elementor-widget-container p').eq(0).remove();
content('img').each(function () {
content(this).attr('src', content(this).attr('data-lazy-src'));
content('img').each((_, el) => {
content(el).attr('src', content(el).attr('data-lazy-src'));
});
item.description = content('.elementor-widget-theme-post-content').html();

View File

@ -47,14 +47,14 @@ async function handler() {
const title = item.text();
let description = '';
item.nextUntil('h4').each(function () {
description += $(this).html();
item.nextUntil('h4').each((_, el) => {
description += $(el).html();
});
if (description === '') {
item.parent()
.nextUntil('h4')
.each(function () {
description += $(this).html();
.each((_, el) => {
description += $(el).html();
});
}

View File

@ -52,8 +52,8 @@ async function handler() {
content('d-contents').remove();
content('img').each(function () {
content(this).attr('src', `${item.link}/${content(this).attr('src')}`);
content('img').each((_, el) => {
content(el).attr('src', `${item.link}/${content(el).attr('src')}`);
});
item.doi = content('meta[name="citation_doi"]').attr('content');

View File

@ -70,8 +70,8 @@ async function handler(ctx) {
content('.article-title').remove();
content('.file-player-image').each(function () {
content(this).replaceWith(`<img src="${content(this).attr('data-actual')}">`);
content('.file-player-image').each((_, el) => {
content(el).replaceWith(`<img src="${content(el).attr('data-actual')}">`);
});
item.description = content('article').html();

View File

@ -103,8 +103,8 @@ async function handler(ctx) {
});
const subContent = load(pageResponse.data);
subContent('img').each(function () {
subContent(this).attr('src', subContent(this).attr('data-url'));
subContent('img').each((_, el) => {
subContent(el).attr('src', subContent(el).attr('data-url'));
});
item.description += subContent('#J-contain_detail_cnt').html();

View File

@ -67,12 +67,13 @@ async function handler(ctx) {
const { data: response } = await got(item.link);
const content = load(response);
// remove all attrs and empty objects
content('div.wp_articlecontent *').each(function () {
if (!content(this).text().trim()) {
return content(this).remove();
content('div.wp_articlecontent *').each((_, el) => {
if (!content(el).text().trim()) {
content(el).remove();
return;
}
for (const attr in this.attribs) {
content(this).removeAttr(attr);
for (const attr in el.attribs) {
content(el).removeAttr(attr);
}
});
const description = content('div.wp_articlecontent').first().html();

View File

@ -26,11 +26,11 @@ async function loadContent(link) {
const firstpost = $('.firstpost');
// 修改图片中的链接
firstpost.find('ignore_js_op img').each(function () {
$(this).attr('src', $(this).attr('file'));
firstpost.find('ignore_js_op img').each((_, el) => {
$(el).attr('src', $(el).attr('file'));
// 移除无用属性
for (const attr of ['id', 'aid', 'zoomfile', 'file', 'zoomfile', 'class', 'onclick', 'title', 'inpost', 'alt', 'onmouseover']) {
$(this).removeAttr(attr);
$(el).removeAttr(attr);
}
});

View File

@ -67,8 +67,8 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('img').each(function () {
content(this).html(`<img src="${content(this).attr('data-src')}">`);
content('img').each((_, el) => {
content(el).html(`<img src="${content(el).attr('data-src')}">`);
});
const image = content('meta[property="og:image"]').attr('content');

View File

@ -60,8 +60,8 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('.futu-news-time-stamp').remove();
content('.nnstock').each(function () {
content(this).replaceWith(`<a href="${content(this).attr('href')}">${content(this).text().replaceAll('$', '')}</a>`);
content('.nnstock').each((_, el) => {
content(el).replaceWith(`<a href="${content(el).attr('href')}">${content(el).text().replaceAll('$', '')}</a>`);
});
item.description = content('.origin_content').html();

View File

@ -86,8 +86,8 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('.futu-news-time-stamp').remove();
content('.nnstock').each(function () {
content(this).replaceWith(`<a href="${content(this).attr('href')}">${content(this).text().replaceAll('$', '')}</a>`);
content('.nnstock').each((_, el) => {
content(el).replaceWith(`<a href="${content(el).attr('href')}">${content(el).text().replaceAll('$', '')}</a>`);
});
item.description = content('.origin_content').html();

View File

@ -86,8 +86,8 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('img').each(function () {
content(this).attr('src', content(this).attr('data-src'));
content('img').each((_, el) => {
content(el).attr('src', content(el).attr('data-src'));
});
item.author = content('.jeg_meta_author').text().replace(/by/, '');

View File

@ -131,19 +131,15 @@ async function handler(ctx) {
const node = $('#content');
// 清理样式
node.find('*')
.filter(function () {
return this.type === 'comment' || this.tagName === 'meta' || this.tagName === 'style';
})
.filter((_, el) => el.type === 'comment' || el.tagName === 'meta' || el.tagName === 'style')
.remove();
node.find('*')
.contents()
.filter(function () {
return this.type === 'comment' || this.tagName === 'meta' || this.tagName === 'style';
})
.filter((_, el) => el.type === 'comment' || el.tagName === 'meta' || el.tagName === 'style')
.remove();
node.find('*').each(function () {
if (this.attribs.style !== undefined) {
const newSty = this.attribs.style
node.find('*').each((_, el) => {
if (el.attribs.style !== undefined) {
const newSty = el.attribs.style
.split(';')
.filter((s) => {
const styBlocklist = ['color:rgb(0,0,0)', 'color:black', 'background:rgb(255,255,255)', 'background:white', 'text-align:left', 'text-align:justify', 'font-style:normal', 'font-weight:normal'];
@ -176,27 +172,27 @@ async function handler(ctx) {
})
.join(';');
if (newSty) {
this.attribs.style = newSty;
el.attribs.style = newSty;
} else {
delete this.attribs.style;
delete el.attribs.style;
}
}
if (this.attribs.class && this.attribs.class.trim().startsWith('Mso')) {
delete this.attribs.class;
if (el.attribs.class && el.attribs.class.trim().startsWith('Mso')) {
delete el.attribs.class;
}
if (this.attribs.lang) {
delete this.attribs.lang;
if (el.attribs.lang) {
delete el.attribs.lang;
}
if (this.tagName === 'font' || this.tagName === 'o:p') {
$(this).replaceWith(this.childNodes);
if (el.tagName === 'font' || el.tagName === 'o:p') {
$(el).replaceWith(el.childNodes);
}
if (this.tagName === 'span' && !this.attribs.style) {
$(this).replaceWith(this.childNodes);
if (el.tagName === 'span' && !el.attribs.style) {
$(el).replaceWith(el.childNodes);
}
});
node.find('span').each(function () {
if (this.childNodes.length === 0) {
$(this).remove();
node.find('span').each((_, el) => {
if (el.childNodes.length === 0) {
$(el).remove();
}
});

View File

@ -45,8 +45,8 @@ async function handler() {
const content = load(detailResponse.data);
content('.wp-block-image').each(function () {
content(this).html(`<img src="${content(this).find('img').attr('data-orig-file')}">`);
content('.wp-block-image').each((_, el) => {
content(el).html(`<img src="${content(el).find('img').attr('data-orig-file')}">`);
});
item.description = content('div[data-widget_type="theme-post-content.default"]').html();

View File

@ -46,8 +46,8 @@ async function getNoticeList(ctx, url, host, titleSelector, dateSelector, conten
item.description = '该通知无法直接预览,请点击原文链接↑查看';
} else {
const $content = load($(contentSelector.content).html());
$content('a').each(function () {
const a = $(this);
$content('a').each((_, el) => {
const a = $(el);
const href = a.attr('href');
if (href && !href.startsWith('http')) {
a.attr('href', new URL(href, host).href);

View File

@ -105,14 +105,14 @@ async function handler(ctx) {
item.description = '';
content('.comtr').each(function () {
const author = content(this).find('.hnuser');
const comment = content(this).find('.commtext');
content('.comtr').each((_, el) => {
const author = content(el).find('.hnuser');
const comment = content(el).find('.commtext');
item.description +=
`<div><div><small><a href="${rootUrl}/${author.attr('href')}">${author.text()}</a></small>` +
`&nbsp&nbsp<small><a href="${rootUrl}/item?id=${content(this).attr('id')}">` +
`${content(this).find('.age').attr('title')}</a></small></div>`;
`&nbsp&nbsp<small><a href="${rootUrl}/item?id=${content(el).attr('id')}">` +
`${content(el).find('.age').attr('title')}</a></small></div>`;
const commentText = comment.clone();

View File

@ -71,9 +71,7 @@ async function handler(ctx: Context): Promise<Data> {
const code = vulData
.find('.code')
.contents()
.filter(function () {
return this.nodeType === 3;
})
.filter((_, el) => el.nodeType === 3)
.text();
const risk = vulData.find('.risk span').eq(1).text();
const vender = vulData.find('.vender').find('.v-name-full').text();

View File

@ -73,10 +73,10 @@ async function handler(ctx) {
const videos = [];
content('.hupu-post-video').each(function () {
content('.hupu-post-video').each((_, el) => {
videos.push({
source: content(this).attr('src'),
poster: content(this).attr('poster'),
source: content(el).attr('src'),
poster: content(el).attr('poster'),
});
});

View File

@ -257,9 +257,9 @@ export function getEntryDetails(item: DataItem): Promise<DataItem> {
.filter(Boolean);
content('.basketballTobbs_tag').remove();
content('.hupu-img').each(function () {
const imgSrc = content(this).attr('data-gif') || content(this).attr('data-origin') || content(this).attr('src');
content(this).parent().html(`<img src="${imgSrc}">`);
content('.hupu-img').each((_, el) => {
const imgSrc = content(el).attr('data-gif') || content(el).attr('data-origin') || content(el).attr('src');
content(el).parent().html(`<img src="${imgSrc}">`);
});
// 分别获取内容元素

View File

@ -54,8 +54,8 @@ async function handler() {
const title = item.text();
let description = '';
item.nextUntil('h2').each(function () {
description += $(this).html();
item.nextUntil('h2').each((_, el) => {
description += $(el).html();
});
return {

View File

@ -76,9 +76,9 @@ const ProcessItems = async (ctx, currentUrl, title) => {
content('#modal-review-watched, #modal-comment-warning, #modal-save-list').remove();
content('.review-buttons, .copy-to-clipboard, .preview-video-container, .play-button').remove();
content('.preview-images img').each(function () {
content(this).removeAttr('data-src');
content(this).attr('src', content(this).parent().attr('href'));
content('.preview-images img').each((_, el) => {
content(el).removeAttr('data-src');
content(el).attr('src', content(el).parent().attr('href'));
});
item.category = content('.panel-block .value a')

View File

@ -73,9 +73,9 @@ const ProcessItems = async (language, currentUrl, tryGet) => {
const content = load(detailResponse.data);
content('.icn_edit, .btn_videoplayer, a[rel="bookmark"]').remove();
content('span').each(function () {
if (content(this).attr('class')?.startsWith('icn_')) {
content(this).remove();
content('span').each((_, el) => {
if (content(el).attr('class')?.startsWith('icn_')) {
content(el).remove();
}
});

View File

@ -48,8 +48,8 @@ async function getItems(ctx, url, host, tableClass, timeStyleClass1, titleStyleC
} else {
const contentHtml = $('.v_news_content').html();
const $content = load(contentHtml);
$content('a').each(function () {
const a = $(this);
$content('a').each((_, el) => {
const a = $(el);
const href = a.attr('href');
if (href && !href.startsWith('http')) {
a.attr('href', new URL(href, host).href);

View File

@ -107,14 +107,14 @@ async function handler(ctx) {
const content = load(detailResponse);
content('a.c-button').each(function () {
content(this).parent().remove();
content('a.c-button').each((_, el) => {
content(el).parent().remove();
});
content('img').each(function () {
content(this).replaceWith(
content('img').each((_, el) => {
content(el).replaceWith(
renderDescription({
src: content(this).prop('src'),
src: content(el).prop('src'),
})
);
});

View File

@ -170,8 +170,8 @@ function generateEnclosureInfo(htmlContent: string): { enclosure_url?: string; e
const $ = load(htmlContent);
let enclosureInfo = {};
$('audio source, video source').each(function () {
const src = $(this).attr('src');
$('audio source, video source').each((_, el) => {
const src = $(el).attr('src');
if (!src) {
return;
}
@ -345,10 +345,10 @@ function processPosts(posts: KemonoPost[], authorName: string, limit: number) {
let replacementCount = 0;
const fanboxRegex = /downloads\.fanbox\.cc/;
$('a').each(function () {
const link = $(this).attr('href');
$('a').each((_, el) => {
const link = $(el).attr('href');
if (link && fanboxRegex.test(link)) {
$(this).replaceWith(kemonoFileElements[replacementCount] || '');
$(el).replaceWith(kemonoFileElements[replacementCount] || '');
replacementCount++;
}
});

View File

@ -58,9 +58,9 @@ async function handler(ctx) {
9
),
};
body.find('a img').each(function () {
$(this).attr('src', $(this).parent('a').attr('href'));
$(this).unwrap();
body.find('a img').each((_, el) => {
$(el).attr('src', $(el).parent('a').attr('href'));
$(el).unwrap();
});
body.find('div.blog_data').remove();
i.description = `<div lang="ja-JP">${body.html()}</div>`;

View File

@ -59,10 +59,10 @@ async function handler(ctx) {
content('.iconfont').remove();
content('img[data-src]').each(function () {
content(this).attr(
content('img[data-src]').each((_, el) => {
content(el).attr(
'src',
content(this)
content(el)
.attr('data-src')
.replace(/_logonews/, '')
);

View File

@ -158,12 +158,12 @@ async function handler(ctx) {
article.find('.inline-embeded-article').remove();
article.find('.relatedArticles').remove();
if (item.inline_attachments) {
article.find('.inlineImage').each(function () {
const inlineImageSrc = $(this).attr('src');
article.find('.inlineImage').each((_, el) => {
const inlineImageSrc = $(el).attr('src');
const inlineAttachment = item.inline_attachments.find((inlineAttachment) => inlineAttachment.url === inlineImageSrc);
if (inlineAttachment && inlineAttachment.description) {
$(this).wrap('<figure></figure>');
$(this).after(`<figcaption>${inlineAttachment.description}</figcaption>`);
$(el).wrap('<figure></figure>');
$(el).after(`<figcaption>${inlineAttachment.description}</figcaption>`);
}
});
}

View File

@ -69,8 +69,8 @@ async function handler(ctx) {
content('hr').nextAll().remove();
content('hr').remove();
content('img').each(function () {
content(this).removeAttr('srcset');
content('img').each((_, el) => {
content(el).removeAttr('srcset');
});
item.description = content('.td-post-content').html();

View File

@ -11,8 +11,8 @@ const getPosts = async (searchParams) => {
const data = await ofetch(url.href);
return data.map((item) => {
const $ = load(item.content.rendered);
$('input').each(function () {
$(this).replaceWith($(this).attr('value') || '');
$('input').each((_, el) => {
$(el).replaceWith($(el).attr('value') || '');
});
$('script').remove();
return {

View File

@ -91,22 +91,22 @@ async function handler(ctx) {
const auhor = info.slice(au_start, au_end).trim();
$('.entry')
.find('span')
.each(function () {
const temp = $(this).text();
$(this).replaceWith(temp);
.each((_, el) => {
const temp = $(el).text();
$(el).replaceWith(temp);
});
$('.entry')
.find('div')
.each(function () {
const temp = $(this).html();
$(this).replaceWith(temp);
.each((_, el) => {
const temp = $(el).html();
$(el).replaceWith(temp);
});
$('.entry').find('a').remove();
$('.entry')
.find('p')
.each(function () {
$(this).removeAttr('style');
$(this).removeAttr('class');
.each((_, el) => {
$(el).removeAttr('style');
$(el).removeAttr('class');
});
$('.wp_art_adjoin').remove();
return { description: $('.entry').html(), date, author: auhor };

View File

@ -71,10 +71,10 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('figure img').each(function () {
content(this)
content('figure img').each((_, el) => {
content(el)
.parent()
.html(`<img src="${content(this).attr('data-src')}">`);
.html(`<img src="${content(el).attr('data-src')}">`);
});
content('.inline-post').remove();

View File

@ -67,8 +67,8 @@ async function handler(ctx) {
const content = load(detailResponse.data);
content('.gatsby-resp-image-link').each(function () {
content(this).html(`<img src="${content(this).find('img').attr('src')}">`);
content('.gatsby-resp-image-link').each((_, el) => {
content(el).html(`<img src="${content(el).find('img').attr('src')}">`);
});
item.description = content('.article__content').html();

View File

@ -71,15 +71,15 @@ async function handler() {
} else {
const $content = $('.wp_articlecontent');
// Convert wp_pdf_player iframes to download links
$content.find('.wp_pdf_player').each(function () {
const $iframe = $(this);
$content.find('.wp_pdf_player').each((_, el) => {
const $iframe = $(el);
const pdfSrc = $iframe.attr('pdfsrc') || '';
const pdfUrl = pdfSrc.startsWith('http') ? pdfSrc : new URL(pdfSrc, host).href;
$iframe.replaceWith(`<p><a href="${pdfUrl}">附件下载</a></p>`);
});
// Fix relative URLs
$content.find('a').each(function () {
const $a = $(this);
$content.find('a').each((_, el) => {
const $a = $(el);
const href = $a.attr('href');
if (href && !href.startsWith('http')) {
$a.attr('href', new URL(href, host).href);

View File

@ -36,15 +36,15 @@ async function getNoticeList(ctx, url, host, titleSelector, dateSelector, conten
} else {
const $content = $(contentSelector.content);
// Convert wp_pdf_player iframes to download links
$content.find('.wp_pdf_player').each(function () {
const $iframe = $(this);
$content.find('.wp_pdf_player').each((_, el) => {
const $iframe = $(el);
const pdfSrc = $iframe.attr('pdfsrc') || '';
const pdfUrl = pdfSrc.startsWith('http') ? pdfSrc : new URL(pdfSrc, host).href;
$iframe.replaceWith(`<p><a href="${pdfUrl}">附件下载</a></p>`);
});
// Fix relative URLs
$content.find('a').each(function () {
const $a = $(this);
$content.find('a').each((_, el) => {
const $a = $(el);
const href = $a.attr('href');
if (href && !href.startsWith('http')) {
$a.attr('href', new URL(href, host).href);

View File

@ -3,8 +3,8 @@ import { load } from 'cheerio';
export function processEmbedPDF(baseurl: string, html: string) {
const $ = load(html);
$('div.wp_pdf_player').each(function () {
const $div = $(this);
$('div.wp_pdf_player').each((_, el) => {
const $div = $(el);
const pdfsrc = $div.attr('pdfsrc') || '';
const downloadUrl = new URL(pdfsrc, baseurl).href;
const newDiv = `<p><a href=${downloadUrl} target="_blank">点击下载 PDF 文件资源</a></p>`;

View File

@ -70,8 +70,8 @@ async function handler() {
const images = detailResponse.data.match(/"url":"[^{}]+","name":"articleLarge"/g).map((e) => JSON.parse(`{${e}}`).url);
let i = 0;
content('figure').each(function () {
content(this).html(
content('figure').each((_, el) => {
content(el).html(
renderToString(
<figure>
<img src={images[i++]} />

View File

@ -56,10 +56,10 @@ async function handler(ctx) {
const content = load(detailResponse.data.match(/"content":"(.*)"}},"secondaryList":/)[1]);
content('img').each(function () {
content(this).attr(
content('img').each((_, el) => {
content(el).attr(
'src',
content(this)
content(el)
.attr('src')
.replaceAll(String.raw`\"`, '')
);

View File

@ -60,10 +60,10 @@ async function handler(ctx) {
const content = load(detailResponse.data.match(/"content":"(.*)","extraction_tags":/)[1]);
content('img').each(function () {
content(this).attr(
content('img').each((_, el) => {
content(el).attr(
'src',
content(this)
content(el)
.attr('src')
.replaceAll(String.raw`\"`, '')
);

View File

@ -18,9 +18,7 @@ const parseArticleContent = ($) => {
const content = $('[itemprop="articleBody"]');
$('*')
.contents()
.filter(function () {
return this.nodeType === 8;
})
.filter((_, el) => el.nodeType === 8)
.remove();
content.find('aside').remove();
content.find('.videoPlayerContainer').remove();

View File

@ -30,29 +30,29 @@ async function sync_detail(link) {
// 存为列表
$(sec_page_selector)
.find(author_selector)
.each(function (i) {
authors[i] = $(this).text();
.each((i, el) => {
authors[i] = $(el).text();
authors[i].replace(' ', ''); // 去空格
authors[i].trim(); // 去首尾空格
});
$(sec_page_selector)
.find(origins_selector)
.each(function (i) {
origins[i] = $(this).text();
.each((i, el) => {
origins[i] = $(el).text();
origins[i].replace(' ', '');
origins[i].trim();
});
$(sec_page_selector)
.find(characters_selector)
.each(function (i) {
characters[i] = $(this).text();
.each((i, el) => {
characters[i] = $(el).text();
characters[i].replace(' ', '');
characters[i].trim();
});
$(sec_page_selector)
.find(tags_selector)
.each(function (i) {
tags[i] = $(this).text();
.each((i, el) => {
tags[i] = $(el).text();
tags[i].replace(' ', '');
tags[i].trim();
});

View File

@ -67,8 +67,8 @@ async function handler(ctx) {
item.enclosure_url = links.eq(-1).attr('href');
item.description = `<video controls><source src="${item.enclosure_url}"></video><br>`;
links.each(function () {
item.description += `<li><a href="${content(this).attr('href')}">${content(this).text()}</a></li>`;
links.each((_, el) => {
item.description += `<li><a href="${content(el).attr('href')}">${content(el).text()}</a></li>`;
});
return item;

View File

@ -64,8 +64,8 @@ async function handler(ctx) {
const authors = [];
content('meta[property="citation_author"]').each(function () {
authors.push(content(this).attr('content'));
content('meta[property="citation_author"]').each((_, el) => {
authors.push(content(el).attr('content'));
});
item.author = authors.join(', ');

View File

@ -38,8 +38,8 @@ async function handler(ctx) {
const $ = load(response.data);
$('a[title="隐藏置顶帖"]').each(function () {
$(this).parents('tbody').remove();
$('a[title="隐藏置顶帖"]').each((_, el) => {
$(el).parents('tbody').remove();
});
let items = $('.s')

View File

@ -51,9 +51,9 @@ async function handler(ctx) {
let $ = load(response);
$('div.capsule__article-image').each(function () {
const image = $(this).find('img').prop('data-original');
$(this).replaceWith(
$('div.capsule__article-image').each((_, el) => {
const image = $(el).find('img').prop('data-original');
$(el).replaceWith(
renderToString(
image ? (
<figure>

View File

@ -39,14 +39,14 @@ const fetchFeed = async (ctx, currentUrl) => {
content('.entry-copyright').remove();
content('.entry-content div').each(function () {
if (/^ruanc-\d+/.test(content(this).attr('id'))) {
content(this).remove();
content('.entry-content div').each((_, el) => {
if (/^ruanc-\d+/.test(content(el).attr('id'))) {
content(el).remove();
}
});
content('figure').each(function () {
content(this).html(`<img src="${content(this).find('a').attr('href')}">`);
content('figure').each((_, el) => {
content(el).html(`<img src="${content(el).find('a').attr('href')}">`);
});
item.description = content('.entry-content').html();

View File

@ -84,16 +84,16 @@ async function fetchContent(url) {
const stubS = subind('<div>');
subind('#postlist')
.find('div[id*="post_"] ')
.each(function () {
if (subind(this).find('td[id*="postmessage_"]').length > 0) {
.each((_, el) => {
if (subind(el).find('td[id*="postmessage_"]').length > 0) {
const section = renderToString(
<DigestSection
author={{
link: subind(this).find('.pls.favatar div.authi a').attr('href'),
name: subind(this).find('.pls.favatar div.authi').text(),
postinfo: subind(this).find('div.authi em[id*=authorposton]').text(),
link: subind(el).find('.pls.favatar div.authi a').attr('href'),
name: subind(el).find('.pls.favatar div.authi').text(),
postinfo: subind(el).find('div.authi em[id*=authorposton]').text(),
}}
msg={subind(this).find('td[id*="postmessage_"]').html()}
msg={subind(el).find('td[id*="postmessage_"]').html()}
host={config.saraba1st.host}
/>
);
@ -101,8 +101,8 @@ async function fetchContent(url) {
}
});
stubS.find('img').each(function () {
const img = subind(this);
stubS.find('img').each((_, el) => {
const img = subind(el);
const file = img.attr('file');
if (file) {
img.attr('src', file);

View File

@ -67,10 +67,10 @@ async function handler(ctx) {
content('h5').parent().remove();
content('div[data-testid="Text-embedded-entry-block"]').remove();
content('img').each(function () {
const image = (content(this).attr('srcset') ?? content(this).attr('src')).split('?w=')[0];
content('img').each((_, el) => {
const image = (content(el).attr('srcset') ?? content(el).attr('src')).split('?w=')[0];
content(this).replaceWith(renderDescription({ image }));
content(el).replaceWith(renderDescription({ image }));
});
item.title = detail.title;

View File

@ -44,8 +44,8 @@ async function handler() {
const response = await got(`${link}?sn_f=1`);
const $ = load(response.data);
const article = $('.left article .htmlview');
article.find('d-image').each(function () {
$(this).replaceWith(`<img src="${$(this).attr('lg')}">`);
article.find('d-image').each((_, el) => {
$(el).replaceWith(`<img src="${$(el).attr('lg')}">`);
});
return {

View File

@ -60,12 +60,12 @@ async function handler(ctx) {
content('.ads-frame, .read-more-msg').remove();
content('figure').each(function () {
content(this).replaceWith(
content('figure').each((_, el) => {
content(el).replaceWith(
renderToString(
<figure>
<img src={content(this).find('img').attr('src')} />
<figcaption>{content(this).find('figcaption').text()}</figcaption>
<img src={content(el).find('img').attr('src')} />
<figcaption>{content(el).find('figcaption').text()}</figcaption>
</figure>
)
);

View File

@ -41,8 +41,8 @@ async function handler(ctx) {
const $ = load(response.data);
$('.align-middle').each(function () {
$(this).removeClass('link-dark');
$('.align-middle').each((_, el) => {
$(el).removeClass('link-dark');
});
let items = $('.link-dark')

View File

@ -78,8 +78,8 @@ async function handler(ctx) {
content('.si-detail__content .si-teaser').remove();
content('.show-for-sr, time, address, .si-detail__translation').remove();
content('picture').each(function () {
content(this).html(`<img src="${content(this).find('source').first().attr('srcset')}">`);
content('picture').each((_, el) => {
content(el).html(`<img src="${content(el).find('source').first().attr('srcset')}">`);
});
item.description = content('.si-detail__content').html();

View File

@ -658,13 +658,13 @@ async function handler(ctx) {
if (msgTypes.includes(UNSUPPORTED)) {
if (unsupportedNodes.length) {
unsupportedHtml += '<blockquote>';
unsupportedNodes.find('.message_media_not_supported_label').each(function () {
const $this = $(this);
unsupportedNodes.find('.message_media_not_supported_label').each((_, el) => {
const $this = $(el);
unsupportedTitle += $this.text();
unsupportedHtml += `<p>${$this.text()}</p>`;
});
unsupportedNodes.find('.message_media_view_in_telegram').each(function () {
const $this = $(this);
unsupportedNodes.find('.message_media_view_in_telegram').each((_, el) => {
const $this = $(el);
unsupportedHtml += $this.attr('href') ? `<p><a href="${$this.attr('href')}">${$this.text()}</a></p>` : `<p>${$this.text()}</p>`;
});
unsupportedHtml += '</blockquote>';

View File

@ -59,10 +59,10 @@ async function handler(ctx) {
content('a[data-sk="tooltip_parent"]').parent().remove();
content('.ad-section, .recommender-title, .navigation-content').remove();
content('.article-img-container').each(function () {
content(this).replaceWith(
content('.article-img-container').each((_, el) => {
content(el).replaceWith(
renderDescription({
image: content(this).find('img')?.attr('data-srcset').split('?')[0] ?? undefined,
image: content(el).find('img')?.attr('data-srcset').split('?')[0] ?? undefined,
})
);
});

View File

@ -37,8 +37,8 @@ async function handler() {
const $ = load(htmlString);
const announceList = [];
$('.mp_news_list > .mp_news_item').each(function () {
const $item = $(this);
$('.mp_news_list > .mp_news_item').each((_, el) => {
const $item = $(el);
const $link = $item.find('a');
const time = $item.find('.read_more').text();
const title = $item.find('strong').text();

View File

@ -40,10 +40,10 @@ async function handler(ctx) {
const chapter_item = [];
$('.ccss>a').each(function () {
$('.ccss>a').each((_, el) => {
chapter_item.push({
title: $(this).text(),
link: `https://www.wenku8.net/novel/${index}/${id}/` + $(this).attr('href'),
title: $(el).text(),
link: `https://www.wenku8.net/novel/${index}/${id}/` + $(el).attr('href'),
});
});

View File

@ -48,9 +48,9 @@ const getItemDetail = async (item, rootUrl) => {
// Missing the `src` properties for the images.
// The `src` property should be replaced with the value of `orisrc` to show the image.
// Replace images in the content with custom JSX template.
content('p.vsbcontent_img').each(function () {
const image = content(this).find('img');
content(this).replaceWith(
content('p.vsbcontent_img').each((_, el) => {
const image = content(el).find('img');
content(el).replaceWith(
renderDescription({
image: {
src: new URL(image.prop('orisrc'), rootUrl).href,
@ -63,8 +63,8 @@ const getItemDetail = async (item, rootUrl) => {
// Missing the `src` properties for the videos.
// The `src` property should be replaced with the value of `vurl` to play the video.
// Replace videos in the content with custom JSX template.
content('script[name="_videourl"]').each(function () {
const video = content(this);
content('script[name="_videourl"]').each((_, el) => {
const video = content(el);
video.replaceWith(
renderDescription({
video: {

View File

@ -42,8 +42,8 @@ const parseArticle = (item) =>
article.find('.bylineWrap').each((i, e) => {
$(e)
.find('p')
.each(function () {
$(this).replaceWith($(this).html());
.each((_, el) => {
$(el).replaceWith($(el).html());
});
});

View File

@ -39,10 +39,10 @@ async function loadContent(link) {
// 图片相对链接处理
$('img').attr('src', (n, v) => new URL(v, baseUrl).href);
// 视频相对链接处理,替换原有播放方法 showVsbVideo
$('.vsbcontent_video').each(function () {
const u1 = $(this).find('script').attr('vurl');
$('.vsbcontent_video').each((_, el) => {
const u1 = $(el).find('script').attr('vurl');
videoUrl = new URL(u1, baseUrl).href;
return $(this)
$(el)
.html('<video width="100%" src="' + videoUrl + '"></video>')
.html();
});

View File

@ -16,8 +16,8 @@ const parseContent = (htmlString) => {
.map((element) => $(element).text().trim());
const content = $('[id^="vsb_content"]');
$('form > div > ul a').each(function () {
$(this).appendTo(content);
$('form > div > ul a').each((_, el) => {
$(el).appendTo(content);
$('<br>').appendTo(content);
});

View File

@ -76,8 +76,8 @@ async function handler(ctx) {
const description = content('div.art-body.wow.fadeInUp')
.clone() // 创建副本防止修改原始内容
.find('ul li') // 定位到附件列表项
.each(function () {
const $li = $(this);
.each((_, el) => {
const $li = $(el);
const newText = $li.html()?.replaceAll(/已下载[\s\S]*?<\/span>次/g, '') ?? '';
$li.html(newText.replace(/<\/a>\s*$/, '</a>'));
})

View File

@ -70,8 +70,8 @@ async function handler(ctx) {
$('#pdfsrc').remove();
$('.bigImg, .smallImg').remove();
$('a img').each(function () {
$(this).parent().remove();
$('a img').each((_, el) => {
$(el).parent().remove();
});
let items = $('.br1, .br2, .titss')

View File

@ -88,8 +88,8 @@ async function handler() {
if (content) {
const $content = load(content);
$content('a').each(function () {
const a = $(this);
$content('a').each((_, el) => {
const a = $(el);
const href = a.attr('href');
if (href && !href.startsWith('http')) {
a.attr('href', new URL(href, baseUrl).href);

View File

@ -82,17 +82,15 @@ async function handler(ctx) {
const $comments = content('.main_article')
.contents()
.filter(function () {
return this.nodeType === 8;
});
$comments.each(function () {
.filter((_, el) => el.nodeType === 8);
$comments.each((_, el) => {
// Remove useless comments
if (/audioPlayer|audio-box/.test(this.data)) {
this.data = '';
if (/audioPlayer|audio-box/.test(el.data)) {
el.data = '';
}
// Filter author from comments
if (/author/.test(this.data)) {
item.author = this.data.split('<author>')[1].split('</author>')[0];
if (/author/.test(el.data)) {
item.author = el.data.split('<author>')[1].split('</author>')[0];
}
});

View File

@ -270,12 +270,12 @@ async function handler(ctx) {
const videos = detailResponse.data.match(/source: '(https:\/\/video\.zcool\.cn\/.*)',/g);
if (videos) {
content('.video-content-box').each(function (i) {
content('.video-content-box').each((i, el) => {
if (i >= videos.length) {
return;
}
content(this).append(
content(el).append(
renderDescription({
video: videos[i].match(/source: '(https:\/\/video\.zcool\.cn\/.*)'/)[1],
})