Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch properties to a JavaScript API to support system themes #171

Merged
merged 1 commit into from
Aug 19, 2023

Conversation

timja
Copy link
Member

@timja timja commented Feb 8, 2023

New JavaScript API, expected use:

if (window.getThemeManagerProperty) {
    setTheme();

    if (window.isSystemRespectingTheme) {
        window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => {
            setTheme()
        });
    }

function setTheme() {
  const theme = window.getThemeManagerProperty('your-plugin-name', 'theme') || 'your-fallback'
}

}

For theme adaption see:
jenkinsci/dark-theme-plugin#385

For plugin adaption see:
jenkinsci/workflow-cps-plugin#769

@timja timja marked this pull request as ready for review August 19, 2023 19:53
@timja timja requested a review from a team as a code owner August 19, 2023 19:53
@timja timja changed the title Add properties to JavaScript API Switch properties to a JavaScript API to support system themes Aug 19, 2023
@timja timja merged commit ada1152 into jenkinsci:master Aug 19, 2023
@timja timja deleted the add-json branch August 19, 2023 20:00
@basil
Copy link
Member

basil commented Aug 21, 2023

======= EXCEPTION START ========
EcmaError: lineNumber=[10] column=[0] lineSource=[<no source>] name=[TypeError] sourceName=[http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js] message=[TypeError: Cannot read property "text" from null (http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js#10)]
org.htmlunit.ScriptException: TypeError: Cannot read property "text" from null (http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js#10)
	at org.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:990)
	at org.htmlunit.corejs.javascript.Context.call(Context.java:590)
	at org.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:484)
	at org.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:348)
	at org.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:868)
	at org.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:844)
	at org.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:114)
	at org.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1043)
	at org.htmlunit.html.ScriptElementSupport.executeScriptIfNeeded(ScriptElementSupport.java:194)
	at org.htmlunit.html.ScriptElementSupport$1.execute(ScriptElementSupport.java:118)
	at org.htmlunit.html.ScriptElementSupport.onAllChildrenAddedToPage(ScriptElementSupport.java:141)
	at org.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:191)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:599)
	at org.htmlunit.cyberneko.xerces.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:412)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:548)
	at org.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1291)
	at org.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1218)
	at org.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:204)
	at org.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:274)
	at org.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:2969)
	at org.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:1953)
	at org.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:834)
	at org.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:346)
	at org.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:297)
	at org.htmlunit.cyberneko.xerces.parsers.XMLParser.parse(XMLParser.java:76)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.parse(HtmlUnitNekoDOMBuilder.java:842)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parse(HtmlUnitNekoHtmlParser.java:202)
	at org.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:301)
	at org.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:220)
	at org.jvnet.hudson.test.HudsonPageCreator.createPage(HudsonPageCreator.java:53)
	at org.htmlunit.WebClient.loadWebResponseInto(WebClient.java:665)
	at org.htmlunit.WebClient.loadWebResponseInto(WebClient.java:567)
	at org.htmlunit.WebClient.getPage(WebClient.java:485)
	at org.htmlunit.WebClient.getPage(WebClient.java:392)
	at org.htmlunit.WebClient.getPage(WebClient.java:530)
	at org.htmlunit.WebClient.getPage(WebClient.java:512)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.goTo(JenkinsRule.java:2628)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.goTo(JenkinsRule.java:2608)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2460)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2338)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2484)
	at org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategyTest.testDontRestrictJobConfiguration(SpecificUsersAuthorizationStrategyTest.java:966)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.jvnet.hudson.test.JenkinsRule$1.evaluate(JenkinsRule.java:606)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.lang.Thread.run(Thread.java:1583)
Caused by: org.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot read property "text" from null (http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js#10)
	at org.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4635)
	at org.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4616)
	at org.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4648)
	at org.htmlunit.corejs.javascript.ScriptRuntime.typeErrorById(ScriptRuntime.java:4653)
	at org.htmlunit.corejs.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4685)
	at org.htmlunit.corejs.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1671)
	at org.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1554)
	at org.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1051)
	at org.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:89)
	at org.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:392)
	at org.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:334)
	at org.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3916)
	at org.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:102)
	at org.htmlunit.javascript.JavaScriptEngine$2.doRun(JavaScriptEngine.java:859)
	at org.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:973)
	... 52 more
Enclosed exception: 
org.htmlunit.corejs.javascript.EcmaError: TypeError: Cannot read property "text" from null (http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js#10)
	at org.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4635)
	at org.htmlunit.corejs.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4616)
	at org.htmlunit.corejs.javascript.ScriptRuntime.typeError(ScriptRuntime.java:4648)
	at org.htmlunit.corejs.javascript.ScriptRuntime.typeErrorById(ScriptRuntime.java:4653)
	at org.htmlunit.corejs.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:4685)
	at org.htmlunit.corejs.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1671)
	at org.htmlunit.corejs.javascript.Interpreter.interpretLoop(Interpreter.java:1554)
	at script(http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js:10)
	at script(http://localhost:37003/jenkins/adjuncts/f41afffc/io/jenkins/plugins/thememanager/header/main.js:1)
	at org.htmlunit.corejs.javascript.Interpreter.interpret(Interpreter.java:1051)
	at org.htmlunit.corejs.javascript.InterpretedFunction.call(InterpretedFunction.java:89)
	at org.htmlunit.corejs.javascript.ContextFactory.doTopCall(ContextFactory.java:392)
	at org.htmlunit.javascript.HtmlUnitContextFactory.doTopCall(HtmlUnitContextFactory.java:334)
	at org.htmlunit.corejs.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3916)
	at org.htmlunit.corejs.javascript.InterpretedFunction.exec(InterpretedFunction.java:102)
	at org.htmlunit.javascript.JavaScriptEngine$2.doRun(JavaScriptEngine.java:859)
	at org.htmlunit.javascript.JavaScriptEngine$HtmlUnitContextAction.run(JavaScriptEngine.java:973)
	at org.htmlunit.corejs.javascript.Context.call(Context.java:590)
	at org.htmlunit.corejs.javascript.ContextFactory.call(ContextFactory.java:484)
	at org.htmlunit.javascript.HtmlUnitContextFactory.callSecured(HtmlUnitContextFactory.java:348)
	at org.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:868)
	at org.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:844)
	at org.htmlunit.javascript.JavaScriptEngine.execute(JavaScriptEngine.java:114)
	at org.htmlunit.html.HtmlPage.loadExternalJavaScriptFile(HtmlPage.java:1043)
	at org.htmlunit.html.ScriptElementSupport.executeScriptIfNeeded(ScriptElementSupport.java:194)
	at org.htmlunit.html.ScriptElementSupport$1.execute(ScriptElementSupport.java:118)
	at org.htmlunit.html.ScriptElementSupport.onAllChildrenAddedToPage(ScriptElementSupport.java:141)
	at org.htmlunit.html.HtmlScript.onAllChildrenAddedToPage(HtmlScript.java:191)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:599)
	at org.htmlunit.cyberneko.xerces.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:412)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.endElement(HtmlUnitNekoDOMBuilder.java:548)
	at org.htmlunit.cyberneko.HTMLTagBalancer.callEndElement(HTMLTagBalancer.java:1291)
	at org.htmlunit.cyberneko.HTMLTagBalancer.endElement(HTMLTagBalancer.java:1218)
	at org.htmlunit.cyberneko.filters.DefaultFilter.endElement(DefaultFilter.java:204)
	at org.htmlunit.cyberneko.filters.NamespaceBinder.endElement(NamespaceBinder.java:274)
	at org.htmlunit.cyberneko.HTMLScanner$ContentScanner.scanEndElement(HTMLScanner.java:2969)
	at org.htmlunit.cyberneko.HTMLScanner$ContentScanner.scan(HTMLScanner.java:1953)
	at org.htmlunit.cyberneko.HTMLScanner.scanDocument(HTMLScanner.java:834)
	at org.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:346)
	at org.htmlunit.cyberneko.HTMLConfiguration.parse(HTMLConfiguration.java:297)
	at org.htmlunit.cyberneko.xerces.parsers.XMLParser.parse(XMLParser.java:76)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoDOMBuilder.parse(HtmlUnitNekoDOMBuilder.java:842)
	at org.htmlunit.html.parser.neko.HtmlUnitNekoHtmlParser.parse(HtmlUnitNekoHtmlParser.java:202)
	at org.htmlunit.DefaultPageCreator.createHtmlPage(DefaultPageCreator.java:301)
	at org.htmlunit.DefaultPageCreator.createPage(DefaultPageCreator.java:220)
	at org.jvnet.hudson.test.HudsonPageCreator.createPage(HudsonPageCreator.java:53)
	at org.htmlunit.WebClient.loadWebResponseInto(WebClient.java:665)
	at org.htmlunit.WebClient.loadWebResponseInto(WebClient.java:567)
	at org.htmlunit.WebClient.getPage(WebClient.java:485)
	at org.htmlunit.WebClient.getPage(WebClient.java:392)
	at org.htmlunit.WebClient.getPage(WebClient.java:530)
	at org.htmlunit.WebClient.getPage(WebClient.java:512)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.goTo(JenkinsRule.java:2628)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.goTo(JenkinsRule.java:2608)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2460)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2338)
	at org.jvnet.hudson.test.JenkinsRule$WebClient.login(JenkinsRule.java:2484)
	at org.jenkinsci.plugins.authorizeproject.strategy.SpecificUsersAuthorizationStrategyTest.testDontRestrictJobConfiguration(SpecificUsersAuthorizationStrategyTest.java:966)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.jvnet.hudson.test.JenkinsRule$1.evaluate(JenkinsRule.java:606)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:299)
	at org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:293)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.lang.Thread.run(Thread.java:1583)
======= EXCEPTION END ========

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants