Skip to content

Conversation

bbeardsley
Copy link
Contributor

No description provided.

@jtojnar
Copy link
Member

jtojnar commented Jan 21, 2018

Could you clarify when would the share url be false or undefined?

@bbeardsley
Copy link
Contributor Author

bbeardsley commented Jan 21, 2018

This would allow me to have a custom function to do my own thing rather than return a url and do the default goto url behavior. For example I want to invoke the android native share api so in my user.js I have:
jQuery(document).ready(function() { if (navigator.share) { selfoss.shares.register('share', 'a', false, function(url, title) { navigator.share({title: title, url: url}); return false; }); } });

@jtojnar
Copy link
Member

jtojnar commented Jan 21, 2018

We could probably get away with something like this:

--- a/public/js/selfoss-shares.js
+++ b/public/js/selfoss-shares.js
@@ -1,7 +1,6 @@
 selfoss.shares = {
     initialized: false,
-    urlBuilders: {},
-    openInNewWindows: {},
+    sharers: {},
     names: {},
     enabledShares: '',
 
@@ -9,42 +8,41 @@
         this.enabledShares = enabledShares;
         this.initialized = true;
 
-        this.register('delicious', 'd', true, function(url, title) {
-            return 'https://delicious.com/save?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
+        this.register('delicious', 'd', function(url, title) {
+            window.open('https://delicious.com/save?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title));
         });
-        this.register('googleplus', 'g', true, function(url) {
-            return 'https://plus.google.com/share?url=' + encodeURIComponent(url);
+        this.register('googleplus', 'g', function(url) {
+            window.open('https://plus.google.com/share?url=' + encodeURIComponent(url));
         });
-        this.register('twitter', 't', true, function(url, title) {
-            return 'https://twitter.com/intent/tweet?source=webclient&text=' + encodeURIComponent(title) + ' ' + encodeURIComponent(url);
+        this.register('twitter', 't', function(url, title) {
+            window.open('https://twitter.com/intent/tweet?source=webclient&text=' + encodeURIComponent(title) + ' ' + encodeURIComponent(url));
         });
-        this.register('facebook', 'f', true, function(url, title) {
-            return 'https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title);
+        this.register('facebook', 'f', function(url, title) {
+            window.open('https://www.facebook.com/sharer/sharer.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title));
         });
-        this.register('pocket', 'p', true, function(url, title) {
-            return 'https://getpocket.com/save?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title);
+        this.register('pocket', 'p', function(url, title) {
+            window.open('https://getpocket.com/save?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title));
         });
-        this.register('wallabag', 'w', true, function(url) {
+        this.register('wallabag', 'w', function(url) {
             if ($('#config').data('wallabag_version') == 2) {
-                return $('#config').data('wallabag') + '/bookmarklet?url=' + encodeURIComponent(url);
+                window.open($('#config').data('wallabag') + '/bookmarklet?url=' + encodeURIComponent(url));
             } else {
-                return $('#config').data('wallabag') + '/?action=add&url=' + btoa(url);
+                window.open($('#config').data('wallabag') + '/?action=add&url=' + btoa(url));
             }
         });
-        this.register('wordpress', 's', true, function(url, title) {
-            return $('#config').data('wordpress') + '/wp-admin/press-this.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title);
+        this.register('wordpress', 's', function(url, title) {
+            window.open($('#config').data('wordpress') + '/wp-admin/press-this.php?u=' + encodeURIComponent(url) + '&t=' + encodeURIComponent(title));
         });
-        this.register('mail', 'e', false, function(url, title) {
-            return 'mailto:?body=' + encodeURIComponent(url) + '&subject=' + encodeURIComponent(title);
+        this.register('mail', 'e', function(url, title) {
+            document.location.href = 'mailto:?body=' + encodeURIComponent(url) + '&subject=' + encodeURIComponent(title);
         });
     },
 
-    register: function(name, id, openInNewWindow, urlBuilder) {
+    register: function(name, id, sharer) {
         if (!this.initialized) {
             return false;
         }
-        this.urlBuilders[name] = urlBuilder;
-        this.openInNewWindows[name] = openInNewWindow;
+        this.sharers[name] = sharer;
         this.names[id] = name;
         return true;
     },
@@ -63,12 +61,7 @@
     },
 
     share: function(name, url, title) {
-        url = this.urlBuilders[name](url, title);
-        if (this.openInNewWindows[name]) {
-            window.open(url);
-        } else {
-            document.location.href = url;
-        }
+        this.sharers[name](url, title);
     },
 
     buildLinks: function(shares, linkBuilder) {

@bbeardsley
Copy link
Contributor Author

Yes, I started down that path but that will break everyone's user.js if they have custom shares registered.

@jtojnar
Copy link
Member

jtojnar commented Jan 21, 2018

I think it is pretty rare and easy to fix. Listing it in changelog should be enough.

@jtojnar jtojnar added this to the 2.19 milestone Mar 9, 2018
@jtojnar
Copy link
Member

jtojnar commented Apr 23, 2018

Closed in 0d75b5c.

@jtojnar jtojnar closed this Apr 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants