From 490764ae11f1e435507e7813dfd5638de6b568d3 Mon Sep 17 00:00:00 2001 From: qsm-odoo Date: Tue, 16 May 2017 15:34:50 +0200 Subject: [PATCH] [FIX] website_blog: fix "select to tweet" feature There was two main problems with the feature - The author name was not properly fetched so the feature which was supposed to add the author of the blog post in the tweet never worked -> this commit simply deletes it - The code was considering the url length in the size of the tweet, so if the url was 135 characters long, it considered that only 5 characters have to be taken in the selected text... which is wrong as twitter URL are all shortened to 23 characters by twitter opw-741530 --- addons/website_blog/static/src/js/contentshare.js | 9 ++++++--- addons/website_blog/static/src/js/website_blog.js | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/addons/website_blog/static/src/js/contentshare.js b/addons/website_blog/static/src/js/contentshare.js index 47cd88fc4a7..cfc02470f7f 100644 --- a/addons/website_blog/static/src/js/contentshare.js +++ b/addons/website_blog/static/src/js/contentshare.js @@ -22,9 +22,12 @@ popover_content += ''; } if($('.blog_title, .blog_content').hasClass('js_tweet')){ - var current_url = window.location.href; - var tweet_selected_text = this.getSelection('string').substring(0,option.maxLength-(current_url.length+option.author_name.length+7)); - var text = encodeURIComponent(_.str.sprintf('\"%s\" --@%s %s', tweet_selected_text, option.author_name, current_url)); + var tweet = '"%s" - %s'; + var baseLength = tweet.replace(/%s/g, '').length; + // Shorten the selected text to match the tweet max length + // Note: all (non-localhost) urls in a tweet have 23 characters https://support.twitter.com/articles/78124 + var selectedText = this.getSelection('string').substring(0, option.maxLength - baseLength - 23); + var text = encodeURIComponent(_.str.sprintf(tweet, selectedText, window.location.href)); popover_content += ''; } return popover_content; diff --git a/addons/website_blog/static/src/js/website_blog.js b/addons/website_blog/static/src/js/website_blog.js index 5c2d82f7646..7985e0ebecc 100644 --- a/addons/website_blog/static/src/js/website_blog.js +++ b/addons/website_blog/static/src/js/website_blog.js @@ -58,7 +58,7 @@ function shareArticle(event){ $(document).ready(function() { if ($('.website_blog').length) { - $(".js_tweet, .js_comment").share({'author_name': $('#blog_author').text()}); + $(".js_tweet, .js_comment").share({}); $('.cover_footer').on('click', page_transist); $('a[href^="#blog_content"]').on('click', animate); $('.o_twitter, .o_facebook, .o_linkedin, .o_google, .o_twitter_complete, .o_facebook_complete, .o_linkedin_complete, .o_google_complete').on('click', shareArticle);