Take away the follow option on the embedded tweet

I need to customize the appearance of embedded tweets on a website by removing the follow button as well as the reply, favorite, and retweet buttons in the footer. Here is a simple HTML example:

<blockquote class="twitter-tweet">
    <a href="https://twitter.com/StackCareers/statuses/468107750333894656"></a>
</blockquote>
<script src="http://platform.twitter.com/widgets.js" charset="utf-8"></script>

Upon inspecting the code, I noticed that the follow button is wrapped in an anchor tag with a specific class:

<a class="follow-button profile" href="https://twitter.com/StackCareers" role="button" data-scribe="component:followbutton" title="Follow StackOverflowCareers on Twitter"><i class="ic-button-bird"></i>Follow</a>

I attempted to remove this class using jQuery:

$('.follow-button.profile').remove()

Additionally, I tried hiding the button with CSS in my stylesheet:

.follow-button {visibility:hidden;}

Following advice from this post, I also added the following line:

.twt-actions { display: none; }

Unfortunately, neither approach worked. Is there a solution for customizing these embedded tweets effectively?

Answer №1

Unfortunately, it is not viable to load cross-domain content into an iframe from a source with a same-origin policy. This means that manipulating the contents using Javascript is not possible.

To my knowledge, there is no readily available embed code for tweets that does not rely on an iframe.

You have two choices in this scenario. The first option is to showcase the tweet as provided without modifications. The second option is to utilize the API to fetch the tweet and customize its display according to your preferences. To pursue the latter route, acquiring an API key is necessary.

Alternatively, you may come across potential workarounds here, but I cannot guarantee the reliability of any implementation based on these suggestions.

Answer №2

After learning about manipulating CSS within Shadow DOM elements, I successfully applied this technique with a widget. The Twitter Widget utilizes a Shadow element named #twitter-widget-0 in my case, where a single tweet is embedded. By incorporating the following snippet into my style sheet:

#twitter-widget-0::shadow div.Tweet-brand {
   display: none;
}

This solution proved to be quite useful for me!

Answer №3

There are various methods to embed tweets - as per the documentation on embedding tweets:

Our widget scans the DOM during execution, converting blockquote.twitter-tweet elements into fully-rendered embedded Tweets based on their content.

To directly render an embedded Tweet at runtime, you can use the twttr.widgets.createTweet() function.

a) Using Default 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>
  &mdash; 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>

You could then locate all the <twitter-widget> elements that were created and update style properties within 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 need to wait for the widget to process each tweet, and there is no direct event to trigger this action at the right time (without resorting to using a setTimeout).

b) Manual JavaScript Implementation

You can customize the creation of the tweet manually like this:

let tweetId = "1188837207206977536"
let tweetContainer = document.getElementById('tweet-container');
twttr.widgets.createTweet(tweetId, tweetContainer)

This will return a promise that allows you to handle the tweet insertion and update styles in the shadowRoot accordingly (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

2<sup>53</sup> - 1 = 9007199254740991
in JavaScript will be truncated (Number.MAX_SAFE_INTEGER)

Insights on Shadow DOM

The <twitter-widget> is a web component styled using the shadow DOM. Global style overrides using /deep/ or ::shadow were deprecated in Chrome 45 and removed in Chrome 63. Presently, setting a style element directly in the element's shadowRoot is the recommended approach to override styles in the shadow root.

Answer №4

if you possess several widgets

<style>

  [id*="twitter-widget"]::shadow div.Tweet-brand {   display: none;  }

</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a reusable anonymous self-invoking function

Here is a function that I am working with: (function(e, t) { var n = function() { //code, code, code }; //code, code, code e.fn.unslider = function(t) { //code, code, code }; })(jQuery, false) To execute this function, I have impleme ...

Managing iframe scrolling using the parent window's scrollbar

I am currently working on an iframe to be utilized across various domains. The functionality of this iframe involves displaying a data list that updates when the bottom of the scroll is reached. An issue I encountered is that the parent window, where the ...

Interacting with content using jQuery

$.ajax({ url: url, type: 'POST', cache: false, data: "param=value", success: function (html) { if (loading_div != '') { document.getElementById(loading_div).innerHTML = "<img src ='" + ful ...

In Chrome, the computed style of background-position is returned as 0% 0%

Let's say I have an element and I am interested in finding out its background-position: To achieve this, I use the following code snippet: window.getComputedStyle(element).getPropertyValue('background-position') If the value of background ...

How to assign a preset value to a text area in AngularJS

While working with PHP and AngularJS, I have a requirement to specify the default value for a textarea. I prefer not to utilize ng-init="textarea = ''" as the data to populate is extensive. Similarly, I want to avoid using AJAX to prevent additi ...

Chosen option from the Bootstrap drop-down menu

Struggling with two problematic input fields on my Bootstrap website... The first is a Bootstrap Typeahead input field: <input type="text" id="typeahead" name='name' placeholder='The name' class="form-control" data-provide="typeahe ...

Tips for implementing jqgrid's inline editing feature using arrow keys

Currently, I have implemented inline editing on jqgrid and it is functioning flawlessly. Whenever the user finishes editing a cell and presses enter, the changes are saved successfully. However, I am now looking to enhance the navigation experience for t ...

Is it possible to load a Twitter widget from dynamically loaded HTML using AJAX?

I've been attempting to implement a standard Twitter search widget directly from the Twitter website: <script src="http://widgets.twimg.com/j/2/widget.js"></script> <script> new TWTR.Widget({ version: 2, type: 'search' ...

Changing base 64 into Mp3 audio format using PHP

I currently have an "audio" tag with a script (which is essentially a plugin) that receives and saves it as base64. This functionality is working properly. My goal now is to modify the script in order to send the base64 data to the server, convert it to m ...

Interactive Icon Feature Instead of Annoying Pop-Ups in JavaScript

Hello there! I need assistance fixing a code issue. Currently, the code automatically pops up when the page is opened. Is there a way to make it clickable instead of popping up automatically? <script type="text/javascript" src="//www.klaviyo.com/media/ ...

The new version 5.1 of Bootstrap modal does not disable scrolling on the <body> element

I'm currently working on a project in Angular 11 using Bootstrap (v5.1.3). My main goal is to create a functionality where clicking on a card, similar to a blog post, will trigger a Bootstrap modal popup displaying the content of that specific post. B ...

Guide to displaying a local HTML file in CKEditor 4.3 using jQuery

Using CKEditor 4.3 on my HTML page, I aim to import content from a local HTML file and display it within the editor. (My apologies for any English errors as I am Brazilian :P) The jQuery code I have tried is as follows: $(document).ready(function(){ ...

Creating pages or tabs within a table using HTML5 and Angular is a simple and effective way to organize

I have a REST response that returns around 500 records. These records are being displayed in an Angular table. I would like to add customization options for the user, allowing them to choose to display 10/20/30... records per page. Additionally, I want to ...

Creating a fixed top bar button in Ionic and iOS along with a scrollable list

In my app using Ionic with the iOS platform, I am trying to achieve a specific layout: A header A fixed bar button A scrollable list Here is what I have attempted so far: <ion-view view-title="Account"> <div class="button-bar" style="margin ...

Ways to Identify When the Back Button is Clicked

Currently, I am developing an HTML website that contains 4 divs on the index.html page. Initially, when clicking on a div, it would expand while the other sections reduced in width. However, the client requested that the URL should change when clicking o ...

Resetting CSS animations using animation-delay

My aim is to animate a series of images on my landing page using Next.js and SCSS. The issue arises when I try to add dynamic animations that reset after each cycle. The delay in the animation causes the reset to also be delayed, which disrupts the flow. I ...

Change web page in JavaScript using post data

Is there a method to utilize JavaScript for navigating to a new URL while including POST parameters? I am aware that with GET requests, you can simply add a parameter string to the URL using window.location.replace(). Is there a way to achieve this with ...

I am having trouble properly positioning an icon next to its corresponding text within a menu item

I'm a newcomer to the world of HTML5 + CSS3 and I'm having trouble with aligning menus, text, and icons within the menu. Here's the issue: Each line of the menu consists of an icon and a text description. The problem is that they are too clo ...

Yii employs the use of quickdlgs to efficiently generate fresh entries within the CGrid view

Within my web application, I am attempting to implement an iframe button using quickdlgs to create new records. However, upon clicking the iframe button, instead of being directed to the 'create' webpage, I am presented with an empty iframe. Here ...

Efficient Pagination in CakePHP Using Ajax Search Functionality with POST Data

I am in the process of implementing a search functionality in my CakePHP 2.x project. Below is the controller code I have written: public function admin_index() { $this->Customer->recursive = 0; $this->Paginator->settings = array( ...