From b8a90778235fbb8d865f9e1c0dfbc82565e5fd64 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 13:07:38 -0700 Subject: [PATCH 1/8] Make HTML templates output Javascript, part 2. #829 --- tools/template.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/template.js b/tools/template.js index 2e361ea93..bdc74278d 100644 --- a/tools/template.js +++ b/tools/template.js @@ -168,7 +168,7 @@ tools.html = function(template) { if (stream.text) { throw new Error(`Unexpected characters in template (${stream.text}): ${template}`); } - return `(innerHTML: \`${output}\`)`; + return `\`{innerHTML: ${output}}\``; }; tools.assert = function(statement) { From e38e6e254bd12ee5769eeb90148c5301eafe0e72 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 16:22:15 -0700 Subject: [PATCH 2/8] Add Header.noticesRoot as early as possible to increase likelihood error messages show up in event of problem. --- src/General/Header.coffee | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/General/Header.coffee b/src/General/Header.coffee index f2f003cab..366b1c6d2 100644 --- a/src/General/Header.coffee +++ b/src/General/Header.coffee @@ -1,5 +1,12 @@ Header = init: -> + $.onExists doc, 'body', => + return unless Main.isThisPageLegit() + $.add @bar, [@noticesRoot, @toggle] + $.prepend d.body, @bar + $.add d.body, Header.hover + @setBarPosition Conf['Bottom Header'] + @menu = new UI.Menu 'header' menuButton = $.el 'span', @@ -86,12 +93,6 @@ Header = @setBoardList() - $.onExists doc, 'body', => - return unless Main.isThisPageLegit() - $.prepend d.body, @bar - $.add d.body, Header.hover - @setBarPosition Conf['Bottom Header'] - $.onExists doc, "#{g.SITE.selectors.boardList} + *", Header.generateFullBoardList Main.ready -> @@ -154,7 +155,7 @@ Header = btn = $('.hide-board-list-button', boardList) $.on btn, 'click', Header.toggleBoardList - $.add Header.bar, [Header.boardList, Header.shortcuts, Header.noticesRoot, Header.toggle] + $.prepend Header.bar, [Header.boardList, Header.shortcuts] Header.setCustomNav Conf['Custom Board Navigation'] Header.generateBoardList Conf['boardnav'] @@ -392,7 +393,7 @@ Header = Header.previousOffset = offsetY setBarPosition: (bottom) -> - Header.barPositionToggler.checked = bottom + Header.barPositionToggler?.checked = bottom $.event 'CloseMenu' args = if bottom then [ 'bottom-header' From 25dd9d59851aa2a12ed0e8b8ba26fd292ec51864 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 16:27:00 -0700 Subject: [PATCH 3/8] Take Coffeescript stuff out of this template. #829 --- src/main/Main.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/Main.coffee b/src/main/Main.coffee index 7cdb7129e..93ded5a02 100644 --- a/src/main/Main.coffee +++ b/src/main/Main.coffee @@ -556,7 +556,7 @@ Main = addDetails data.error.stack.replace(data.error.toString(), '').trim() if data.error.stack addDetails '\n`' + data.html + '`' if data.html details = details.replace /file:\/{3}.+\//g, '' # Remove local file paths - url = "<%= meta.newIssue.replace('%title', '#{encodeURIComponent title}').replace('%details', '#{encodeURIComponent details}') %>" + url = '<%= meta.newIssue %>'.replace('%title', encodeURIComponent title).replace('%details', encodeURIComponent details) <%= html(' [report]') %> isThisPageLegit: -> From 48022bb31ba84396d9e4e3e0f57540371515a4f9 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 17:49:24 -0700 Subject: [PATCH 4/8] Remove last uses of $.globalEval. --- src/General/Header.coffee | 3 ++- src/Miscellaneous/Fourchan.coffee | 11 +++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/General/Header.coffee b/src/General/Header.coffee index 366b1c6d2..054d5ba8c 100644 --- a/src/General/Header.coffee +++ b/src/General/Header.coffee @@ -103,7 +103,8 @@ Header = $('#navtopright', footer).id = 'navbotright' $('#settingsWindowLink', footer).id = 'settingsWindowLinkBot' $.before absbot, footer - $.globalEval 'window.cloneTopNav = function() {};' + $.global -> + window.cloneTopNav = -> if (Header.bottomBoardList = $ g.SITE.selectors.boardListBottom) for a in $$ 'a', Header.bottomBoardList a.className = 'current' if a.hostname is location.hostname and a.pathname.split('/')[1] is g.BOARD.ID diff --git a/src/Miscellaneous/Fourchan.coffee b/src/Miscellaneous/Fourchan.coffee index 186576ec4..ec46de04e 100644 --- a/src/Miscellaneous/Fourchan.coffee +++ b/src/Miscellaneous/Fourchan.coffee @@ -12,17 +12,16 @@ Fourchan = unless $.hasClass pre, 'prettyprinted' pre.innerHTML = e.detail.html $.addClass pre, 'prettyprinted' - $.globalEval ''' - window.addEventListener('prettyprint', function(e) { + $.global -> + window.addEventListener('prettyprint', (e) -> window.dispatchEvent(new CustomEvent('prettyprint:cb', { detail: { ID: e.detail.ID, i: e.detail.i, - html: prettyPrintOne(e.detail.html) + html: window.prettyPrintOne(e.detail.html) } - })); - }, false); - ''' + })) + , false) Callbacks.Post.push name: 'Parse [code] tags' cb: Fourchan.code From 5f4ae3231f7a758bf3b77383bd18f66636e19157 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 18:12:06 -0700 Subject: [PATCH 5/8] Remove $.globalEval and simplify passing of data to/from scripts run in global context. --- src/platform/$.coffee | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/platform/$.coffee b/src/platform/$.coffee index 4c309ee5b..d2da191a6 100644 --- a/src/platform/$.coffee +++ b/src/platform/$.coffee @@ -416,19 +416,19 @@ $.queueTask = do -> taskQueue.push arguments setTimeout execTask, 0 -$.globalEval = (code, data) -> - script = $.el 'script', - textContent: code - $.extend script.dataset, data if data - $.add (d.head or doc), script - $.rm script - $.global = (fn, data) -> if doc - $.globalEval "(#{fn})();", data + script = $.el 'script', + textContent: "(#{fn}).call(document.currentScript.dataset);" + $.extend script.dataset, data if data + $.add (d.head or doc), script + $.rm script + script.dataset else # XXX dwb - fn() + try + fn.call(data) + data $.bytesToString = (size) -> unit = 0 # Bytes From 53f2252f38252631b3e0510f0e5ab114a938079c Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 18:49:57 -0700 Subject: [PATCH 6/8] Detect conflicts with native extension and show appropriate error message. #1627 --- src/main/Main.coffee | 11 +++++++++++ src/site/SW.yotsuba.coffee | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/src/main/Main.coffee b/src/main/Main.coffee index 93ded5a02..9b600b920 100644 --- a/src/main/Main.coffee +++ b/src/main/Main.coffee @@ -99,6 +99,7 @@ Main = # Fresh install else if !items.previousversion? + Main.isFirstRun = true Main.ready -> $.set 'previousversion', g.VERSION Settings.open() @@ -502,6 +503,16 @@ Main = new Notice 'error', 'Error: Multiple copies of 4chan X are enabled.' $.addClass doc, 'tainted' + # Detect conflicts with native extension + if g.SITE.testNativeExtension and not $.hasClass(doc, 'tainted') + {enabled} = g.SITE.testNativeExtension() + if enabled + $.addClass doc, 'tainted' + if Conf['Disable Native Extension'] and !Main.isFirstRun + msg = $.el 'div', + <%= html('Failed to disable the native extension. You may need to block it.') %> + new Notice 'error', msg + unless errors instanceof Array error = errors else if errors.length is 1 diff --git a/src/site/SW.yotsuba.coffee b/src/site/SW.yotsuba.coffee index 44521b85a..c29f51cfe 100644 --- a/src/site/SW.yotsuba.coffee +++ b/src/site/SW.yotsuba.coffee @@ -225,3 +225,7 @@ SW.yotsuba = isLinkified: (link) -> ImageHost.test(link.hostname) + + testNativeExtension: -> + $.global -> + @enabled = 'true' if window.Parser.postMenuIcon From 51a6893fddcfeea0072cc9745bd6db5fc68013b6 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 18:56:43 -0700 Subject: [PATCH 7/8] Don't try to disable 4chan native extension on non-4chan sites. --- src/General/Settings.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/General/Settings.coffee b/src/General/Settings.coffee index cccf7968b..80bc72c53 100644 --- a/src/General/Settings.coffee +++ b/src/General/Settings.coffee @@ -21,7 +21,7 @@ Settings = $.on d, 'AddSettingsSection', Settings.addSection $.on d, 'OpenSettings', (e) -> Settings.open e.detail - if Conf['Disable Native Extension'] + if g.SITE.software is 'yotsuba' and Conf['Disable Native Extension'] if $.hasStorage # Run in page context to handle case where 4chan X has localStorage access but not the page. # (e.g. Pale Moon 26.2.2, GM 3.8, cookies disabled for 4chan only) From 375c717dea3ff136d8ebea8b938e9d90bd474c64 Mon Sep 17 00:00:00 2001 From: ccd0 Date: Thu, 25 Jul 2019 19:20:53 -0700 Subject: [PATCH 8/8] Replace assert with Test.assert. --- src/General/Test.coffee | 4 ++++ src/Linkification/Linkify.coffee | 5 ++++- tools/template.js | 4 ---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/General/Test.coffee b/src/General/Test.coffee index a60c14f3a..acffd0146 100644 --- a/src/General/Test.coffee +++ b/src/General/Test.coffee @@ -18,6 +18,10 @@ Test = Header.menu.addEntry el: a2 + assert: (condition) -> + unless condition() + new Notice 'warning', "Assertion failed: #{condition}", 30 + normalize: (root) -> root2 = root.cloneNode true for el in $$ '.mobile', root2 diff --git a/src/Linkification/Linkify.coffee b/src/Linkification/Linkify.coffee index d1abed37a..7a82bd1a7 100644 --- a/src/Linkification/Linkify.coffee +++ b/src/Linkification/Linkify.coffee @@ -71,7 +71,10 @@ Linkify = if Linkify.regString.test word links.push Linkify.makeRange node, endNode, index, length - <% if (readJSON('/.tests_enabled')) { %><%= assert('word is links[links.length-1].toString()') %><% } %> + <% if (readJSON('/.tests_enabled')) { %> + Test.assert -> + word is links[links.length-1].toString() + <% } %> break unless test.lastIndex and node is endNode diff --git a/tools/template.js b/tools/template.js index bdc74278d..b75cab4cc 100644 --- a/tools/template.js +++ b/tools/template.js @@ -171,10 +171,6 @@ tools.html = function(template) { return `\`{innerHTML: ${output}}\``; }; -tools.assert = function(statement) { - return `throw new Error 'Assertion failed: ' + \`${constExpression(statement)}\` unless ${statement}`; -}; - function includesDir(templateName) { var dir = path.dirname(templateName); var subdir = path.basename(templateName).replace(/\.[^.]+$/, '');