Files
RSSHub/lib/utils/common-utils.js
Henry Wang d09affa703 feat: add addNoReferrer in common-utils (#2571)
* fix: no-referrer tag

* chore: merge qdaily routes

* feat: add addNoReferrer in common-utils

This adds no-referrer attribute to images.
Parameters:

- add no-referrer attribute to images
- $: cheerio selector
- source: source selector, string
- target: target attribute name, typically for lazyload imgs, string
- srcPrefix: prefix for src, string
- removeAttr: attributes to remove, array: ['attrA','attrB']

* test: add test case

* doc: update
2019-07-08 11:45:02 +08:00

46 lines
1.1 KiB
JavaScript

// convert a string into title case
const toTitleCase = (str) =>
str
.toLowerCase()
.split(' ')
.map((word) => word.replace(word[0], word[0].toUpperCase()))
.join(' ');
// add no-referrer attribute to images
// $: cheerio selector
// source: source selector, string
// target: target attribute name, typically for lazyload imgs, string
// srcPrefix: prefix for src, string
// removeAttr: attributes to remove, array: ['attrA','attrA']
const addNoReferrer = ($, source, target, srcPrefix, removeAttr) => {
$(`${source} img`).each((index, e) => {
const $e = $(e);
let src = target ? $e.attr(target) : $e.attr('src');
if (src) {
if (srcPrefix) {
src = srcPrefix + src;
}
if (target) {
$e.removeAttr(target);
}
if (removeAttr) {
removeAttr.forEach((e) => {
$e.removeAttr(e);
});
}
$e.attr('src', src);
$e.attr('referrerpolicy', 'no-referrer');
}
});
};
module.exports = {
toTitleCase,
addNoReferrer,
};