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

Dynamically change theme? #170

Closed
robbiet480 opened this issue Jun 10, 2019 · 3 comments
Closed

Dynamically change theme? #170

robbiet480 opened this issue Jun 10, 2019 · 3 comments
Labels

Comments

@robbiet480
Copy link

@robbiet480 robbiet480 commented Jun 10, 2019

Is there any way to change the theme from JavaScript? I see the configurator sends a message like {type: "set-theme", theme: "github-dark"} using postMessage but am unable to get this working on my own site. For now, I'll have to resort to reloading utterances whenever the user changes the theme. Would be great if this was actually supported though.

@jdanyow jdanyow added the question label Jun 23, 2019
@jdanyow

This comment has been minimized.

Copy link
Member

@jdanyow jdanyow commented Jun 23, 2019

I think it works. The key is to pass https://utteranc.es in the second arg of the postMessage call (the targetOrigin parameter) because the message is targeted at the utterances iframe.

const message = {
  type: 'set-theme',
  theme: 'github-dark'
};
var utterances = document.querySelector('iframe');
utterances.contentWindow.postMessage(message, 'https://utteranc.es');
@jdanyow jdanyow closed this Jul 7, 2019
@Jakeler

This comment has been minimized.

Copy link

@Jakeler Jakeler commented Mar 3, 2020

How to do it on page load? I want to restore theme settings from localstorage with JS.
The problem is that nothing happens, apparently because the message comes to early. So far i have tried running it from a event listener on window DOMContentLoaded or load, which did not work:

main.js:44 Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://utteranc.es') does not match the recipient window's origin ('http://myurl.xyz')

Then even from an iframe.onload function, it shows no error, but nothing changes.
It works in my tests only if triggered later manually.

@jdanyow

This comment has been minimized.

Copy link
Member

@jdanyow jdanyow commented Mar 3, 2020

@Jakeler try this:

// wait for utterances to load and send it's first message.
addEventListener('message', event => {
  if (event.origin !== 'https://utteranc.es') {
    return;
  }
  const message = {
    type: 'set-theme',
    theme: 'github-dark'
  };
  const utterances = document.querySelector('iframe').contentWindow; // try event.source instead
  utterances.postMessage(message, 'https://utteranc.es');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.