Resolve even more templating issues before decaffinating

This commit is contained in:
Tuxedo Takodachi 2023-02-23 21:19:18 +01:00
parent 166ecb42f7
commit 60ae31ead0
9 changed files with 81 additions and 72 deletions

6
decaffinate.md Normal file
View File

@ -0,0 +1,6 @@
# migration from coffeescript to modern js/ts
## TODO
- look for TODO comments
- find alternative for `<% if (`

View File

@ -385,7 +385,8 @@ Header =
Header.previousOffset = offsetY Header.previousOffset = offsetY
setBarPosition: (bottom) -> setBarPosition: (bottom) ->
Header.barPositionToggler?.checked = bottom # TODO check if barPositionToggler exists
Header.barPositionToggler.checked = bottom
$.event 'CloseMenu' $.event 'CloseMenu'
args = if bottom then [ args = if bottom then [
'bottom-header' 'bottom-header'

View File

@ -1,4 +1,4 @@
<% if (readJSON('/.tests_enabled')) { %> # <% if (readJSON('/.tests_enabled')) { %>
Test = Test =
init: -> init: ->
return unless g.SITE.software is 'yotsuba' and g.VIEW in ['index', 'thread'] return unless g.SITE.software is 'yotsuba' and g.VIEW in ['index', 'thread']
@ -169,4 +169,4 @@ Test =
Test.testAll() Test.testAll()
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
<% } %> # <% } %>

View File

@ -71,10 +71,10 @@ Linkify =
if Linkify.regString.test word if Linkify.regString.test word
links.push Linkify.makeRange node, endNode, index, length links.push Linkify.makeRange node, endNode, index, length
<% if (readJSON('/.tests_enabled')) { %> # <% if (readJSON('/.tests_enabled')) { %>
Test.assert -> # Test.assert ->
word is links[links.length-1].toString() # word is links[links.length-1].toString()
<% } %> # <% } %>
break unless test.lastIndex and node is endNode break unless test.lastIndex and node is endNode

View File

@ -16,12 +16,12 @@ import Mayhem_unreadSFW from './Favicon/Mayhem.unreadSFW.png';
import Mayhem_unreadSFWY from './Favicon/Mayhem.unreadSFWY.png'; import Mayhem_unreadSFWY from './Favicon/Mayhem.unreadSFWY.png';
import Mayhem_unreadNSFW from './Favicon/Mayhem.unreadNSFW.png'; import Mayhem_unreadNSFW from './Favicon/Mayhem.unreadNSFW.png';
import Mayhem_unreadNSFWY from './Favicon/Mayhem.unreadNSFWY.png'; import Mayhem_unreadNSFWY from './Favicon/Mayhem.unreadNSFWY.png';
import 4chanJS_unreadDead from './Favicon/4chanJS.unreadDead.png'; import fourChanJS_unreadDead from './Favicon/4chanJS.unreadDead.png';
import 4chanJS_unreadDeadY from './Favicon/4chanJS.unreadDeadY.png'; import fourChanJS_unreadDeadY from './Favicon/4chanJS.unreadDeadY.png';
import 4chanJS_unreadSFW from './Favicon/4chanJS.unreadSFW.png'; import fourChanJS_unreadSFW from './Favicon/4chanJS.unreadSFW.png';
import 4chanJS_unreadSFWY from './Favicon/4chanJS.unreadSFWY.png'; import fourChanJS_unreadSFWY from './Favicon/4chanJS.unreadSFWY.png';
import 4chanJS_unreadNSFW from './Favicon/4chanJS.unreadNSFW.png'; import fourChanJS_unreadNSFW from './Favicon/4chanJS.unreadNSFW.png';
import 4chanJS_unreadNSFWY from './Favicon/4chanJS.unreadNSFWY.png'; import fourChanJS_unreadNSFWY from './Favicon/4chanJS.unreadNSFWY.png';
import Original_unreadDead from './Favicon/Original.unreadDead.png'; import Original_unreadDead from './Favicon/Original.unreadDead.png';
import Original_unreadDeadY from './Favicon/Original.unreadDeadY.png'; import Original_unreadDeadY from './Favicon/Original.unreadDeadY.png';
import Original_unreadSFW from './Favicon/Original.unreadSFW.png'; import Original_unreadSFW from './Favicon/Original.unreadSFW.png';
@ -85,12 +85,12 @@ Favicon =
Mayhem_unreadNSFWY, Mayhem_unreadNSFWY,
] ]
'4chanJS': [ '4chanJS': [
4chanJS_unreadDead, fourChanJS_unreadDead,
4chanJS_unreadDeadY, fourChanJS_unreadDeadY,
4chanJS_unreadSFW, fourChanJS_unreadSFW,
4chanJS_unreadSFWY, fourChanJS_unreadSFWY,
4chanJS_unreadNSFW, fourChanJS_unreadNSFW,
4chanJS_unreadNSFWY, fourChanJS_unreadNSFWY,
] ]
Original: [ Original: [
Original_unreadDead, Original_unreadDead,

View File

@ -2,9 +2,9 @@ class Post
toString: -> @ID toString: -> @ID
constructor: (root, @thread, @board, flags={}) -> constructor: (root, @thread, @board, flags={}) ->
<% if (readJSON('/.tests_enabled')) { %> # <% if (readJSON('/.tests_enabled')) { %>
@normalizedOriginal = Test.normalize root # @normalizedOriginal = Test.normalize root
<% } %> # <% } %>
$.extend @, flags $.extend @, flags
@ID = +root.id.match(/\d*$/)[0] @ID = +root.id.match(/\d*$/)[0]
@ -57,9 +57,9 @@ class Post
@isHidden = false @isHidden = false
@clones = [] @clones = []
<% if (readJSON('/.tests_enabled')) { %> # <% if (readJSON('/.tests_enabled')) { %>
return if @forBuildTest # return if @forBuildTest
<% } %> # <% } %>
if g.posts.get(@fullID) if g.posts.get(@fullID)
@isRebuilt = true @isRebuilt = true
@clones = g.posts.get(@fullID).clones @clones = g.posts.get(@fullID).clones

View File

@ -130,8 +130,9 @@ Main =
items.previousversion = changes.previousversion = g.VERSION items.previousversion = changes.previousversion = g.VERSION
$.set changes, -> $.set changes, ->
if items['Show Updated Notifications'] ? true if items['Show Updated Notifications'] ? true
# TODO meta
el = $.el 'span', el = $.el 'span',
`<%= html(meta.name + ' has been updated to <a href="' + meta.changelog + '" target="_blank">version ${g.VERSION}</a>.') %>` `{ innerHTML: 'meta.name has been updated to <a href="' + meta.changelog + '" target="_blank">version ${g.VERSION}</a>.'}`
new Notice 'info', el, 15 new Notice 'info', el, 15
parseURL: (site=g.SITE, url=location) -> parseURL: (site=g.SITE, url=location) ->
@ -535,7 +536,7 @@ Main =
$.addClass doc, 'tainted' $.addClass doc, 'tainted'
if Conf['Disable Native Extension'] and !Main.isFirstRun if Conf['Disable Native Extension'] and !Main.isFirstRun
msg = $.el 'div', msg = $.el 'div',
`<%= html('Failed to disable the native extension. You may need to <a href="' + meta.faq + '#blocking-native-extension" target="_blank">block it</a>.') %>` `{innerHTML: 'Failed to disable the native extension. You may need to <a href="' + meta.faq + '#blocking-native-extension" target="_blank">block it</a>.'}`
new Notice 'error', msg new Notice 'error', msg
unless errors instanceof Array unless errors instanceof Array
@ -579,7 +580,8 @@ Main =
title += " (+#{errors.length - 1} other errors)" if errors.length > 1 title += " (+#{errors.length - 1} other errors)" if errors.length > 1
details = '' details = ''
addDetails = (text) -> addDetails = (text) ->
unless encodeURIComponent(title + details + text + '\n').length > `<%= meta.newIssueMaxLength - meta.newIssue.replace(/%(title|details)/, '').length %>` # TODO meta
unless encodeURIComponent(title + details + text + '\n').length > `"meta.newIssueMaxLength - meta.newIssue.replace(/%(title|details)/, '')".length`
details += text + '\n' details += text + '\n'
addDetails """ addDetails """
[Please describe the steps needed to reproduce this error.] [Please describe the steps needed to reproduce this error.]
@ -704,6 +706,6 @@ Main =
['Mod Contact Links', ModContact] ['Mod Contact Links', ModContact]
] ]
<% if (readJSON('/.tests_enabled')) { %> # <% if (readJSON('/.tests_enabled')) { %>
Main.features.push ['Build Test', Test] # Main.features.push ['Build Test', Test]
<% } %> # <% } %>

View File

@ -77,11 +77,11 @@ $.ajax = do ->
options.type or= options.form and 'post' or 'get' options.type or= options.form and 'post' or 'get'
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310 # XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?(?:4chan|4channel|4cdn)\.org)\/adv\//, '$1//adv/' url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?(?:4chan|4channel|4cdn)\.org)\/adv\//, '$1//adv/'
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
# XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638 # # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638
if Conf['Work around CORB Bug'] and g.SITE.software is 'yotsuba' and !options.testCORB and FormData.prototype.entries # if Conf['Work around CORB Bug'] and g.SITE.software is 'yotsuba' and !options.testCORB and FormData.prototype.entries
return $.ajaxPage url, options # return $.ajaxPage url, options
<% } %> # <% } %>
{onloadend, timeout, responseType, withCredentials, type, onprogress, form, headers} = options {onloadend, timeout, responseType, withCredentials, type, onprogress, form, headers} = options
r = new pageXHR() r = new pageXHR()
try try
@ -92,12 +92,12 @@ $.ajax = do ->
$.extend r.upload, {onprogress} $.extend r.upload, {onprogress}
# connection error or content blocker # connection error or content blocker
$.on r, 'error', -> (c.warn "4chan X failed to load: #{url}" unless r.status) $.on r, 'error', -> (c.warn "4chan X failed to load: #{url}" unless r.status)
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
# XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638 # # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638
$.on r, 'load', -> # $.on r, 'load', ->
if !Conf['Work around CORB Bug'] and r.readyState is 4 and r.status is 200 and r.statusText is '' and r.response is null # if !Conf['Work around CORB Bug'] and r.readyState is 4 and r.status is 200 and r.statusText is '' and r.response is null
$.set 'Work around CORB Bug', (Conf['Work around CORB Bug'] = Date.now()) # $.set 'Work around CORB Bug', (Conf['Work around CORB Bug'] = Date.now())
<% } %> # <% } %>
r.send form r.send form
catch err catch err
# XXX Some content blockers in Firefox (e.g. Adblock Plus and NoScript) throw an exception instead of simulating a connection error. # XXX Some content blockers in Firefox (e.g. Adblock Plus and NoScript) throw an exception instead of simulating a connection error.
@ -107,9 +107,9 @@ $.ajax = do ->
$.queueTask $.event, 'loadend', null, r $.queueTask $.event, 'loadend', null, r
r r
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
# XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638 # # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638
do -> # do ->
requestID = 0 requestID = 0
requests = $.dict() requests = $.dict()
@ -179,7 +179,7 @@ do ->
form = Array.from(form.entries()) if form form = Array.from(form.entries()) if form
$.event '4chanXAjax', {url, timeout, responseType, withCredentials, type, onprogress: !!onprogress, form, headers, id} $.event '4chanXAjax', {url, timeout, responseType, withCredentials, type, onprogress: !!onprogress, form, headers, id}
req req
<% } %> # <% } %>
# Status Code 304: Not modified # Status Code 304: Not modified
# With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses. # With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses.
@ -357,13 +357,13 @@ $.one = (el, events, handler) ->
$.on el, events, cb $.on el, events, cb
$.event = (event, detail, root=d) -> $.event = (event, detail, root=d) ->
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
if detail? and typeof cloneInto is 'function' if detail? and typeof cloneInto is 'function'
detail = cloneInto detail, d.defaultView detail = cloneInto detail, d.defaultView
<% } %> # <% } %>
root.dispatchEvent new CustomEvent event, {bubbles: true, cancelable: true, detail} root.dispatchEvent new CustomEvent event, {bubbles: true, cancelable: true, detail}
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
# XXX Make $.event work in Pale Moon with GM 3.x (no cloneInto function). # XXX Make $.event work in Pale Moon with GM 3.x (no cloneInto function).
do -> do ->
return unless /PaleMoon\//.test(navigator.userAgent) and +GM_info?.version?.split('.')[0] >= 2 and typeof cloneInto is 'undefined' return unless /PaleMoon\//.test(navigator.userAgent) and +GM_info?.version?.split('.')[0] >= 2 and typeof cloneInto is 'undefined'
@ -383,12 +383,12 @@ do ->
obj obj
$.event = (event, detail, root=d) -> $.event = (event, detail, root=d) ->
root.dispatchEvent new CustomEvent event, {bubbles: true, cancelable: true, detail: clone detail} root.dispatchEvent new CustomEvent event, {bubbles: true, cancelable: true, detail: clone detail}
<% } %> # <% } %>
$.modifiedClick = (e) -> $.modifiedClick = (e) ->
e.shiftKey or e.altKey or e.ctrlKey or e.metaKey or e.button isnt 0 e.shiftKey or e.altKey or e.ctrlKey or e.metaKey or e.button isnt 0
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
$.open = $.open =
if GM?.openInTab? if GM?.openInTab?
GM.openInTab GM.openInTab
@ -396,10 +396,10 @@ $.open =
GM_openInTab GM_openInTab
else else
(url) -> window.open url, '_blank' (url) -> window.open url, '_blank'
<% } else { %> # <% } else { %>
$.open = $.open =
(url) -> window.open url, '_blank' (url) -> window.open url, '_blank'
<% } %> # <% } %>
$.debounce = (wait, fn) -> $.debounce = (wait, fn) ->
lastCall = 0 lastCall = 0
@ -527,7 +527,7 @@ $.securityCheck = (data) ->
if location.protocol isnt 'https:' if location.protocol isnt 'https:'
delete data['Redirect to HTTPS'] delete data['Redirect to HTTPS']
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
# https://developer.chrome.com/extensions/storage.html # https://developer.chrome.com/extensions/storage.html
$.oldValue = $.oldValue =
local: $.dict() local: $.dict()
@ -646,7 +646,7 @@ do ->
cb? err unless --count cb? err unless --count
chrome.storage.local.clear done chrome.storage.local.clear done
chrome.storage.sync.clear done chrome.storage.sync.clear done
<% } else { %> # <% } else { %>
# http://wiki.greasespot.net/Main_Page # http://wiki.greasespot.net/Main_Page
# https://tampermonkey.net/documentation.php # https://tampermonkey.net/documentation.php
@ -809,4 +809,4 @@ else
$.delete $.listValues().map (key) -> key.replace g.NAMESPACE, '' $.delete $.listValues().map (key) -> key.replace g.NAMESPACE, ''
cb?() cb?()
<% } %> # <% } %>

View File

@ -1,4 +1,4 @@
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
eventPageRequest = do -> eventPageRequest = do ->
callbacks = [] callbacks = []
chrome.runtime.onMessage.addListener (response) -> chrome.runtime.onMessage.addListener (response) ->
@ -8,17 +8,17 @@ eventPageRequest = do ->
chrome.runtime.sendMessage params, (id) -> chrome.runtime.sendMessage params, (id) ->
callbacks[id] = cb callbacks[id] = cb
<% } %> # <% } %>
CrossOrigin = CrossOrigin =
binary: (url, cb, headers=$.dict()) -> binary: (url, cb, headers=$.dict()) ->
# XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310 # XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?(?:4chan|4channel|4cdn)\.org)\/adv\//, '$1//adv/' url = url.replace /^((?:https?:)?\/\/(?:\w+\.)?(?:4chan|4channel|4cdn)\.org)\/adv\//, '$1//adv/'
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
eventPageRequest {type: 'ajax', url, headers, responseType: 'arraybuffer'}, ({response, responseHeaderString}) -> eventPageRequest {type: 'ajax', url, headers, responseType: 'arraybuffer'}, ({response, responseHeaderString}) ->
response = new Uint8Array(response) if response response = new Uint8Array(response) if response
cb response, responseHeaderString cb response, responseHeaderString
<% } %> # <% } %>
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
fallback = -> fallback = ->
$.ajax url, { $.ajax url, {
headers headers
@ -57,7 +57,7 @@ CrossOrigin =
(GM?.xmlHttpRequest or GM_xmlhttpRequest) gmOptions (GM?.xmlHttpRequest or GM_xmlhttpRequest) gmOptions
catch catch
fallback() fallback()
<% } %> # <% } %>
file: (url, cb) -> file: (url, cb) ->
CrossOrigin.binary url, (data, headers) -> CrossOrigin.binary url, (data, headers) ->
@ -112,15 +112,15 @@ CrossOrigin =
{onloadend, timeout, responseType, headers} = options {onloadend, timeout, responseType, headers} = options
responseType ?= 'json' responseType ?= 'json'
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest? unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
return $.ajax url, options return $.ajax url, options
<% } %> # <% } %>
req = new CrossOrigin.Request() req = new CrossOrigin.Request()
req.onloadend = onloadend req.onloadend = onloadend
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
gmOptions = { gmOptions = {
method: 'GET' method: 'GET'
url url
@ -153,14 +153,14 @@ CrossOrigin =
req.abort = -> req.abort = ->
try try
gmReq.abort() gmReq.abort()
<% } %> # <% } %>
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
eventPageRequest {type: 'ajax', url, responseType, headers, timeout}, (result) -> eventPageRequest {type: 'ajax', url, responseType, headers, timeout}, (result) ->
if result.status if result.status
$.extend req, result $.extend req, result
req.onloadend() req.onloadend()
<% } %> # <% } %>
req req
@ -168,15 +168,15 @@ CrossOrigin =
$.cache url, cb, $.cache url, cb,
ajax: CrossOrigin.ajax ajax: CrossOrigin.ajax
<% if (type === 'crx') { %> # <% if (type === 'crx') { %>
permission: (cb, cbFail, origins) -> permission: (cb, cbFail, origins) ->
eventPageRequest {type: 'permission', origins}, (result) -> eventPageRequest {type: 'permission', origins}, (result) ->
if result if result
cb() cb()
else else
cbFail() cbFail()
<% } %> # <% } %>
<% if (type === 'userscript') { %> # <% if (type === 'userscript') { %>
permission: (cb) -> permission: (cb) ->
cb() cb()
<% } %> # <% } %>