Add configuration for domain_replacements

This commit is contained in:
3np 2021-04-06 00:12:31 +09:00
parent 649132ef94
commit a2cab737f8
6 changed files with 22 additions and 8 deletions

View File

@ -27,6 +27,9 @@ const config = {
nsfw_enabled: process.env.NSFW_ENABLED !== 'true' || true, // Enable NSFW (over 18) content. If false, a warning is shown to the user before opening any NSFW post. When the NFSW content is disabled, NSFW posts are hidden from subreddits and from user page feeds. Note: Users can set this to true or false from their preferences.
post_comments_sort: process.env.POST_COMMENTS_SORT || 'confidence', // "confidence" is the default sorting in Reddit. Must be one of: confidence, top, new, controversial, old, random, qa, live.
reddit_app_id: process.env.REDDIT_APP_ID || 'ABfYqdDc9qPh1w', // If "use_reddit_oauth" config key is set to true, you have to obtain your Reddit app ID. For testing purposes it's okay to use this project's default app ID. Create your Reddit app here: https://old.reddit.com/prefs/apps/. Make sure to create an "installed app" type of app.
domain_replacements: process.env.DOMAIN_REPLACEMENTS
? (JSON.parse(process.env.DOMAIN_REPLACEMENTS).map(([p, r]) => [new RegExp(p, 'gm'), r]))
: [], // Replacements for domains in outgoing links. Tuples with regular expressions to match, and replacement values. This is in addition to user-level configuration of privacyDomains.
post_media_max_heights: {
/**
* Sets the max-height value for images and videos in posts.

View File

@ -55,7 +55,7 @@ module.exports = function(request, fs) {
}
} catch(e) { }
url = replacePrivacyDomains(url, user_preferences)
url = replaceDomains(url, user_preferences)
return url
}
@ -159,7 +159,7 @@ module.exports = function(request, fs) {
return unescaped[m]
})
result = replacePrivacyDomains(result, user_preferences)
result = replaceDomains(result, user_preferences)
return result
} else {
@ -167,9 +167,20 @@ module.exports = function(request, fs) {
}
}
this.replacePrivacyDomains = (str, user_preferences) => {
this.replaceDomains = (str, user_preferences) => {
if(typeof(str) == 'undefined' || !str)
return
if (config.domain_replacements) {
for (replacement of config.domain_replacements) {
str = str.replace(...replacement)
}
}
return this.replaceUserDomains(str, user_preferences)
}
this.replaceUserDomains = (str, user_preferences) => {
let redditRegex = /([A-z.]+\.)?(reddit(\.com)|redd(\.it))/gm;
let youtubeRegex = /([A-z.]+\.)?youtu(be\.com|\.be)/gm;

View File

@ -91,7 +91,7 @@ module.exports = function() {
title: data.title,
ups: data.ups,
upvote_ratio: data.upvote_ratio,
url: replacePrivacyDomains(data.url, user_preferences),
url: replaceDomains(data.url, user_preferences),
stickied: data.stickied,
is_self_link: is_self_link,
subreddit_front: subreddit_front,

View File

@ -64,7 +64,7 @@ module.exports = function() {
title: post.title,
created: post.created_utc,
ups: post.ups,
url: replacePrivacyDomains(url, user_preferences),
url: replaceDomains(url, user_preferences),
thumbnail: await downloadAndSave(post.thumbnail),
duration: duration,
edited: post.edited,
@ -83,7 +83,7 @@ module.exports = function() {
created: post.created_utc,
subreddit_name_prefixed: post.subreddit_name_prefixed,
ups: post.ups,
url: replacePrivacyDomains(url, user_preferences),
url: replaceDomains(url, user_preferences),
edited: post.edited,
body_html: unescape(post.body_html),
num_comments: post.num_comments,

View File

@ -70,7 +70,7 @@ module.exports = function() {
link_flair_text: post.link_flair_text,
ups: post.ups,
images: images,
url: replacePrivacyDomains(post.url, user_preferences),
url: replaceDomains(post.url, user_preferences),
edited: post.edited,
selftext_html: unescape(post.body_html),
num_comments: post.num_comments,

View File

@ -36,7 +36,7 @@ module.exports = function() {
display_name: data.display_name,
display_name_prefixed: data.display_name_prefixed,
public_description: data.public_description,
url: replacePrivacyDomains(data.url, user_preferences),
url: replaceDomains(data.url, user_preferences),
subscribers: data.subscribers,
over_18: data.over18,
title: data.title,