There are a couple different ways to embed tweets - according to the docs on embedding tweets:
Our widget scans the DOM on execution, converting blockquote.twitter-tweet
elements into fully-rendered embedded Tweets based on element content.
To directly render an embedded Tweet at runtime use the twttr.widgets.createTweet()
function.
a) Default Through Publish Widget
If you process this tweet through the publish widget, it will generate the following code (demo):
<blockquote class="twitter-tweet">
<p lang="en" dir="ltr">Nobody:<br><br>Software Marketing Page: "Blazingly Fast"</p>
— Kyle Mitofsky (@KyleMitBTV)
<a href="https://twitter.com/KyleMitBTV/status/1188837207206977536">October 28, 2019</a>
</blockquote>
<script src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
Then, you could go identify all the <twitter-widget>
elements that were created and update any style properties in their shadowRoot
like this:
[...document.getElementsByTagName('twitter-widget')].forEach(tweet => {
let style = tweet.shadowRoot.firstElementChild;
let css = document.createTextNode(`.EmbeddedTweet .CallToAction { display: none; }`);
style.appendChild(css);
})
However, we have to wait until the widget has processed each tweet and I can't find an event to tap into that would allow you to fire this at the right time (without doing some weird setTimeout
stuff and hoping you waited long enough
b) Manually through JavaScript
You can take over the creation of the tweet like this:
let tweetId = "1188837207206977536"
let tweetContainer = document.getElementById('tweet-container');
twttr.widgets.createTweet(tweetId, tweetContainer)
Which will return a promise that you can handle as soon as the tweet has been inserted, allowing you to update styles in the shadowRoot
like this (demo):
<div id="tweet-container" class="embedded-tweets"></div>
<script src="https://platform.twitter.com/widgets.js"></script>
<script type="text/javascript">
let tweetId = "1188837207206977536"
let tweetContainer = document.getElementById('tweet-container');
twttr.widgets.createTweet(tweetId, tweetContainer)
.then(function (tweet) {
let style = tweet.shadowRoot.firstElementChild;
let css = document.createTextNode(`.EmbeddedTweet .CallToAction { display: none; }`);
style.appendChild(css);
});
</script>
Note: The Tweet ID must be a string as numbers above <code>2<sup>53</sup> - 1 = 9007199254740991</code> in JavaScript will be truncated (Number.MAX_SAFE_INTEGER
)
Notes on Shadow DOM
The <twitter-widget>
is a web component that is styled using the shadow DOM. Global style overrides using /deep/
or ::shadow
were deprecated in Chrome 45 and removed in Chrome 63. Currently the best way to override styles in shadow root is to set a style
element directly in the element's shadowRoot