From 3fc0490b96ddfd2ee7fea0e2111732c438dd048b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Mon, 18 May 2020 17:22:09 +0200 Subject: [PATCH] Add fallback link to Twitter media provider (#3309) * Add fallback link to Twitter media provider Closes #3308 * Fix TwitterEmbed error for invalid Tweet IDs * Add TwitterEmbed#embedAll * Fix typo in TwitterEmbed#embedTweet comment * Fix code style in TwitterEmbed.js Co-authored-by: Matthias Schmidt Co-authored-by: Matthias Schmidt --- com.woltlab.wcf/mediaProvider.xml | 5 +- .../Core/Ui/Message/TwitterEmbed.js | 62 +++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/TwitterEmbed.js diff --git a/com.woltlab.wcf/mediaProvider.xml b/com.woltlab.wcf/mediaProvider.xml index 4f065ecaaa..71fa49551d 100644 --- a/com.woltlab.wcf/mediaProvider.xml +++ b/com.woltlab.wcf/mediaProvider.xml @@ -78,9 +78,8 @@ https?://www.twitch.tv/[a-zA-Z0-9]+/v/(?[0-9]+)]]> Twitter Tweet [0-9a-zA-Z_]+)/status/(?[0-9]+)]]> - window.twttr=function(t,e,r){var n,i=t.getElementsByTagName(e)[0],w=window.twttr||{};return t.getElementById(r)?w:((n=t.createElement(e)).id=r,n.src="https://platform.twitter.com/widgets.js",i.parentNode.insertBefore(n,i),w._e=[],w.ready=function(t){w._e.push(t)},w)}(document,"script","twitter-wjs"); -
-]]> + https://twitter.com/{$USERNAME}/status/{$ID}/ +]]>
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/TwitterEmbed.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/TwitterEmbed.js new file mode 100644 index 0000000000..3c45916a1b --- /dev/null +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/TwitterEmbed.js @@ -0,0 +1,62 @@ +/** + * Wrapper around Twitter's createTweet API. + * + * @author Tim Duesterhus + * @copyright 2001-2020 WoltLab GmbH + * @license GNU Lesser General Public License + * @module WoltLabSuite/Core/Ui/Message/TwitterEmbed + */ +define(['https://platform.twitter.com/widgets.js'], function(Widgets) { + "use strict"; + + var twitterReady = new Promise(function(resolve, reject) { + twttr.ready(resolve); + }); + + /** + * @exports WoltLabSuite/Core/Ui/Message/TwitterEmbed + */ + return { + /** + * Embed the tweet identified by the given tweetId into the given container. + * + * @param {HTMLElement} container + * @param {string} tweetId + * @param {boolean} removeChildren Whether to remove existing children of the given container after embedding the tweet. + * @return {HTMLElement} The Tweet element created by Twitter. + */ + embedTweet: function(container, tweetId, removeChildren) { + if (removeChildren === undefined) removeChildren = false; + + return twitterReady.then(function() { + return twttr.widgets.createTweet(tweetId, container, { + dnt: true, + lang: document.documentElement.lang, + }); + }).then(function(tweet) { + if (tweet && removeChildren) { + while (container.lastChild) { + container.removeChild(container.lastChild); + } + container.appendChild(tweet); + } + + return tweet; + }); + }, + + /** + * Embeds tweets into all elements with a data-wsc-twitter-tweet attribute, removing + * existing children. + */ + embedAll: function() { + elBySelAll("[data-wsc-twitter-tweet]", undefined, function(container) { + var tweetId = elData(container, "wsc-twitter-tweet"); + if (tweetId) { + this.embedTweet(container, tweetId, true); + elData(container, "wsc-twitter-tweet", ""); + } + }.bind(this)) + } + }; +}); -- 2.20.1