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

Add support for updating config on-the-fly with postMessage #138

Merged
merged 5 commits into from
Jul 11, 2021

Conversation

laymonage
Copy link
Member

@laymonage laymonage commented Jul 11, 2021

Fix #134.

This PR adds support for updating giscus config on-the-fly with iframe.contentWindow.postMessage.

Example usage:

interface ISetConfigMessage {
  setConfig: {
    theme?: string;
    repo?: string;
    term?: string;
    number?: number;
    category?: string;
    reactionsEnabled?: boolean;
    emitMetadata?: boolean;
  };
}

function sendMessage<T>(message: T) {
  const iframe = document.querySelector<HTMLIFrameElement>('iframe.giscus-frame');
  if (!iframe) return;
  iframe.contentWindow.postMessage({ giscus: message }, location.origin);
}

sendMessage({
  setConfig: {
    theme: 'https://giscus.app/themes/custom_example.css',
    reactionsEnabled: false,
  }
} as ISetConfigMessage);

This can be used to dynamically change giscus theme based on the parent window's theme (utterance/utterances#427, utterance/utterances#549). It also lets you change the discussion without having to reload the <iframe> or <script> (utterance/utterances#359).

@vercel
Copy link

vercel bot commented Jul 11, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/laymonage/giscus/6bDKaVjJzsFsZYKQrhU1yhxTV94B
✅ Preview: https://giscus-git-setconfig-message-laymonage.vercel.app

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

Successfully merging this pull request may close these issues.

Add a client API?
1 participant