Support specifying the sites that filters apply to. #2171

This commit is contained in:
ccd0 2019-04-06 05:57:16 -07:00
parent edc8cf6d35
commit f3ed76f32f
7 changed files with 48 additions and 34 deletions

View File

@ -7,9 +7,6 @@ Filter =
unless Conf['Filtered Backlinks']
$.addClass doc, 'hide-backlinks'
nsfwBoards = BoardConfig.sfwBoards(false).join(',')
sfwBoards = BoardConfig.sfwBoards(true).join(',')
for key of Config.filter
for line in Conf[key].split '\n'
continue if line[0] is '#'
@ -20,26 +17,11 @@ Filter =
# Don't mix up filter flags with the regular expression.
filter = line.replace regexp[0], ''
# Comma-separated list of the boards this filter applies to.
# Defaults to global.
boardsRaw = filter.match(/boards:([^;]+)/)?[1].toLowerCase()
if boardsRaw
boards = {}
for board in boardsRaw.replace('nsfw', nsfwBoards).replace('sfw', sfwBoards).split(',')
boards[board] = true
else
boards = false
# List of the boards this filter applies to.
boards = @parseBoards filter.match(/boards:([^;]+)/)?[1]
# boards to exclude from an otherwise global rule
# due to the sfw and nsfw keywords, also works on all filters
# replaces 'nsfw' and 'sfw' for consistency
excludesRaw = filter.match(/exclude:([^;]+)/)?[1].toLowerCase()
if excludesRaw
excludes = {}
for board in excludesRaw.replace('nsfw', nsfwBoards).replace('sfw', sfwBoards).split(',')
excludes[board] = true
else
excludes = false
# Boards to exclude from an otherwise global rule.
excludes = @parseBoards filter.match(/exclude:([^;]+)/)?[1]
if (isstring = (key in ['uniqueID', 'MD5']))
# MD5 filter will use strings instead of regular expressions.
@ -111,6 +93,25 @@ Filter =
name: 'Filter'
cb: @node
# Parse comma-separated list of boards.
# Sites can be specified by a beginning part of the site domain followed by a colon.
parseBoards: (boardsRaw) ->
return false unless boardsRaw
boardsRaw = boardsRaw.toLowerCase()
boards = {}
siteFilter = ''
for boardID in boardsRaw.split(',')
if ':' in boardID
[siteFilter, boardID] = boardID.split(':')[-2..]
for siteID, siteProperties of Conf['siteProperties']
continue if siteProperties.canonical or siteID[...siteFilter.length] isnt siteFilter
if boardID in ['nsfw', 'sfw']
for boardID2 of SW[siteProperties.software]?.sfwBoards?(boardID is 'sfw') or []
boards["#{siteID}/#{boardID2}"] = true
else
boards["#{siteID}/#{boardID}"] = true
boards
test: (post, hideable=true) ->
return post.filterResults if post.filterResults
hide = false
@ -122,12 +123,14 @@ Filter =
hideable = false
mask = (if post.isReply then 2 else 1)
mask = (mask | (if post.file then 4 else 8))
board = "#{post.siteID}/#{post.boardID}"
site = "#{post.siteID}/*"
for key of Filter.filters when ((value = Filter.value key, post)?)
# Continue if there's nothing to filter (no tripcode for example).
for filter in Filter.filters[key]
continue if (
(filter.boards and !filter.boards[post.boardID]) or
(filter.excludes and filter.excludes[post.boardID]) or
(filter.boards and !(filter.boards[board] or filter.boards[site] )) or
(filter.excludes and (filter.excludes[board] or filter.excludes[site])) or
(filter.mask & mask) or
(if filter.isstring then (filter.regexp isnt value) else !filter.regexp.test(value))
)

View File

@ -31,12 +31,13 @@ Build =
else
"/#{boardID}/thread/#{threadID}#p#{postID}"
parseJSON: (data, boardID) ->
parseJSON: (data, boardID, siteID) ->
o =
# id
ID: data.no
threadID: data.resto or data.no
boardID: boardID
siteID: siteID or Site.hostname
isReply: !!data.resto
# thread status
isSticky: !!data.sticky

View File

@ -9,9 +9,14 @@
<li>
Per boards, separate them with commas. It is global if not specified. Use <code>sfw</code> and <code>nsfw</code> to reference all worksafe or not-worksafe boards.<br>
For example: <code>boards:a,jp;</code>.<br>
To specify boards on a particular site, put the beginning of the domain and a slash character before the list.<br>
Any initial <code>www.</code> should not be included, and all 4chan domains are considered <code>4chan.org</code>.<br>
For example: <code>boards:4:a,jp,sama:a,z;</code>.<br>
An asterisk can be used to specify all boards on a site.<br>
For example: <code>boards:4:*;</code>.<br>
</li>
<li>
In case of a global rule or one that uses <code>sfw</code>/<code>nsfw</code>, select boards to be excluded from the filter.<br>
Select boards to be excluded from the filter. The syntax is the same as for the <code>boards:</code> option above.<br>
For example: <code>exclude:vg,v;</code>.
</li>
<li>

View File

@ -349,7 +349,7 @@ ThreadWatcher =
unread++
if !quotingYou and !Conf['Require OP Quote Link'] and youOP and not Filter.isHidden(Build.parseJSON postObj, boardID)
if !quotingYou and !Conf['Require OP Quote Link'] and youOP and not Filter.isHidden(Build.parseJSON postObj, boardID, siteID)
quotingYou = true
continue
@ -367,7 +367,7 @@ ThreadWatcher =
}
quotesYou = true
break
if quotesYou and not Filter.isHidden(Build.parseJSON postObj, boardID)
if quotesYou and not Filter.isHidden(Build.parseJSON postObj, boardID, siteID)
quotingYou = true
ThreadWatcher.update siteID, boardID, threadID, {last, replies, isDead, unread, quotingYou}

View File

@ -9,6 +9,7 @@ class Post
@ID = +root.id.match(/\d*$/)[0]
@threadID = @thread.ID
@boardID = @board.ID
@siteID = Site.hostname
@fullID = "#{@board}.#{@ID}"
@context = @
@isReply = (@ID isnt @threadID)

View File

@ -151,3 +151,6 @@ SW.yotsuba =
hasCORS: (url) ->
url.split('/')[...3].join('/') is location.protocol + '//a.4cdn.org'
sfwBoards: (sfw) ->
BoardConfig.sfwBoards(sfw)

View File

@ -1,17 +1,19 @@
Site =
defaultProperties:
'4chan.org': {software: 'yotsuba'}
'4channel.org': {software: 'yotsuba'}
'4cdn.org': {software: 'yotsuba'}
'4channel.org': {canonical: '4chan.org'}
'4cdn.org': {canonical: '4chan.org'}
init: (cb) ->
$.extend Conf['siteProperties'], Site.defaultProperties
{hostname} = location
while hostname and hostname not of Conf['siteProperties']
hostname = hostname.replace(/^[^.]*\.?/, '')
if hostname and Conf['siteProperties'][hostname].software of SW
@set hostname
cb()
if hostname
hostname = canonical if (canonical = Conf['siteProperties'][hostname].canonical)
if Conf['siteProperties'][hostname].software of SW
@set hostname
cb()
$.onExists doc, 'body', =>
for software of SW when (changes = SW[software].detect?())
changes.software = software
@ -32,5 +34,4 @@ Site =
set: (@hostname) ->
@properties = Conf['siteProperties'][@hostname]
@software = @properties.software
@hostname = '4chan.org' if @software is 'yotsuba'
$.extend @, SW[@software]