Support style switcher and non-default styles on Tinyboard.

This commit is contained in:
ccd0 2018-12-06 09:14:59 -08:00
parent c9906c488e
commit da6c1c4e4f
3 changed files with 61 additions and 46 deletions

View File

@ -233,33 +233,39 @@ Main =
Main.setClass() Main.setClass()
setClass: -> setClass: ->
if g.VIEW is 'catalog' knownStyles = ['yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'photon', 'tomorrow', 'spooky']
$.addClass doc, $.id('base-css').href.match(/catalog_(\w+)/)[1].replace('_new', '').replace /_+/g, '-'
if Site.software is 'yotsuba' and g.VIEW is 'catalog'
if (mainStyleSheet = $.id('base-css'))
style = mainStyleSheet.href.match(/catalog_(\w+)/)?[1].replace('_new', '').replace(/_+/g, '-')
if style in knownStyles
$.addClass doc, style
return return
style = 'yotsuba-b' style = mainStyleSheet = styleSheets = null
mainStyleSheet = $ 'link[title=switch]', d.head
styleSheets = $$ 'link[rel="alternate stylesheet"]', d.head
setStyle = -> setStyle = ->
# Use preconfigured CSS for 4chan's default themes.
if Site.software is 'yotsuba'
$.rmClass doc, style $.rmClass doc, style
style = null style = null
for styleSheet in styleSheets for styleSheet in styleSheets
if styleSheet.href is mainStyleSheet?.href if styleSheet.href is mainStyleSheet?.href
style = styleSheet.title.toLowerCase().replace('new', '').trim().replace /\s+/g, '-' style = styleSheet.title.toLowerCase().replace('new', '').trim().replace /\s+/g, '-'
style = styleSheet.href.match(/[a-z]*(?=[^/]*$)/)[0] if style is '_special' style = styleSheet.href.match(/[a-z]*(?=[^/]*$)/)[0] if style is '_special'
style = null unless style in ['yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'photon', 'tomorrow', 'spooky'] style = null unless style in knownStyles
break break
if style if style
$.addClass doc, style $.addClass doc, style
$.rm Main.bgColorStyle $.rm Main.bgColorStyle
else return
# Determine proper background color for dialogs if 4chan is using a special stylesheet.
# Determine proper dialog background color for other themes.
div = Site.bgColoredEl() div = Site.bgColoredEl()
div.style.position = 'absolute'; div.style.position = 'absolute';
div.style.visibility = 'hidden'; div.style.visibility = 'hidden';
$.add d.body, div $.add d.body, div
bgColor = window.getComputedStyle(div).backgroundColor bgColor = window.getComputedStyle(div).backgroundColor
c.log(bgColor)
$.rm div $.rm div
rgb = bgColor.match(/[\d.]+/g) rgb = bgColor.match(/[\d.]+/g)
# Use body background if reply background is transparent # Use body background if reply background is transparent
@ -275,12 +281,19 @@ Main =
} }
""" """
$.after $.id('fourchanx-css'), Main.bgColorStyle $.after $.id('fourchanx-css'), Main.bgColorStyle
setStyle()
return unless mainStyleSheet $.onExists d.head, Site.selectors.styleSheet, (el) ->
mainStyleSheet = el
if Site.software is 'yotsuba'
styleSheets = $$ 'link[rel="alternate stylesheet"]', d.head
new MutationObserver(setStyle).observe mainStyleSheet, { new MutationObserver(setStyle).observe mainStyleSheet, {
attributes: true attributes: true
attributeFilter: ['href'] attributeFilter: ['href']
} }
$.on mainStyleSheet, 'load', setStyle
setStyle()
unless mainStyleSheet
setStyle()
initReady: -> initReady: ->
if Site.is404?() if Site.is404?()

View File

@ -88,6 +88,7 @@ SW.tinyboard =
spoiler: '.spoiler' spoiler: '.spoiler'
quotelink: 'a[onclick^="highlightReply("]' quotelink: 'a[onclick^="highlightReply("]'
boardList: '.boardlist' boardList: '.boardlist'
styleSheet: '#stylesheet'
xpath: xpath:
thread: 'div[starts-with(@id,"thread_")]' thread: 'div[starts-with(@id,"thread_")]'

View File

@ -39,6 +39,7 @@ SW.yotsuba =
spoiler: 's' spoiler: 's'
quotelink: ':not(pre) > .quotelink' # XXX https://github.com/4chan/4chan-JS/issues/77: 4chan currently creates quote links inside [code] tags; ignore them quotelink: ':not(pre) > .quotelink' # XXX https://github.com/4chan/4chan-JS/issues/77: 4chan currently creates quote links inside [code] tags; ignore them
boardList: '#boardNavDesktop > .boardList' boardList: '#boardNavDesktop > .boardList'
styleSheet: 'link[title=switch]'
xpath: xpath:
thread: 'div[contains(concat(" ",@class," ")," thread ")]' thread: 'div[contains(concat(" ",@class," ")," thread ")]'