Skip to content

Commit

Permalink
More robost Mastodon share implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
QbDesu committed Dec 31, 2024
1 parent 57a3c1e commit d50b760
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions app/assets/javascripts/mastodon-share.js
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 1 addition & 1 deletion app/assets/stylesheets/frontend/pages/_show.scss
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ body.page-show {
float: left;
margin-right: 10px;

a {
a, button {
display: block;
color: white;

Expand Down
4 changes: 0 additions & 4 deletions app/helpers/frontend/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion app/views/frontend/shared/_embedshare.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
})
});

0 comments on commit d50b760

Please sign in to comment.