From d50b760b1bcb257655b1a5c8fe315c85fd1453b9 Mon Sep 17 00:00:00 2001 From: Qb Date: Tue, 31 Dec 2024 16:39:08 +0100 Subject: [PATCH] More robost Mastodon share implementation --- app/assets/javascripts/application.js | 1 + app/assets/javascripts/mastodon-share.js | 14 ++++++++++++++ app/assets/stylesheets/frontend/pages/_show.scss | 2 +- app/helpers/frontend/application_helper.rb | 4 ---- app/views/frontend/shared/_embedshare.haml | 5 ++++- 5 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/mastodon-share.js diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 5bcfdab3..75e25d3b 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -21,6 +21,7 @@ //= require jquery.bxslider-v4.2.1d-ssfrontend //= require slider //= require feed_toggle +//= require mastodon-share //= require mirrorbrain-fix //= require mediaelement-and-player diff --git a/app/assets/javascripts/mastodon-share.js b/app/assets/javascripts/mastodon-share.js new file mode 100644 index 00000000..4e74e3d7 --- /dev/null +++ b/app/assets/javascripts/mastodon-share.js @@ -0,0 +1,14 @@ +function mastodonShare(text, url) { + const enteredDomain = prompt("Please enter the domain of your mastodon instance, e.g. chaos.social").trim(); + if (!enteredDomain) return; + + const domainURL = "https://" + enteredDomain.replace("https://", ""); + + const shareURL = new URL(domainURL); + shareURL.pathname = "/share"; + if (text) shareURL.searchParams.set("text", text); + if (url) shareURL.searchParams.set("url", url); + + const windowHandle = window.open(shareURL, "_blank"); + if (windowHandle) windowHandle.focus(); +} \ No newline at end of file diff --git a/app/assets/stylesheets/frontend/pages/_show.scss b/app/assets/stylesheets/frontend/pages/_show.scss index 2c92cc61..5d218358 100644 --- a/app/assets/stylesheets/frontend/pages/_show.scss +++ b/app/assets/stylesheets/frontend/pages/_show.scss @@ -81,7 +81,7 @@ body.page-show { float: left; margin-right: 10px; - a { + a, button { display: block; color: white; diff --git a/app/helpers/frontend/application_helper.rb b/app/helpers/frontend/application_helper.rb index a5c14bb3..d49275b3 100644 --- a/app/helpers/frontend/application_helper.rb +++ b/app/helpers/frontend/application_helper.rb @@ -6,10 +6,6 @@ def oembed_api_event_url(event) (public_oembed_url(url: event_url(slug: event.slug))).freeze end - def mastodon_url(title, url) - 'javascript:window.location.href = "https://" + prompt("Please enter the domain of your mastodon instance, e.g. chaos.social") + "/share?url='.freeze + URI.encode_www_form_component(url) + '&text='.freeze + URI.encode_www_form_component(title) + '"'.freeze - end - def facebook_url(title, url) 'https://www.facebook.com/sharer/sharer.php?t='.freeze + URI.encode_www_form_component(title) + '&u=' + URI.encode_www_form_component(url) end diff --git a/app/views/frontend/shared/_embedshare.haml b/app/views/frontend/shared/_embedshare.haml index 7441c95b..0cdb0997 100644 --- a/app/views/frontend/shared/_embedshare.haml +++ b/app/views/frontend/shared/_embedshare.haml @@ -10,7 +10,7 @@ %ul.share %li - %a{:href => mastodon_url(event.title, event_url(slug: event.slug)), :title => 'via Mastodon'} + %button{data: {share: 'mastodon', text: event.title, url: event_url(slug: event.slug)}} %i{:class => "icon-mastodon"} %li @@ -35,4 +35,7 @@ $("textarea.share").on('focus', function() { $(this).select(); }); + $('.share').on('click', '[data-share=mastodon]', function (e) { + mastodonShare(this.dataset.text, this.dataset.url); + }) });