• With 3.x it was easy to change the current page url/title/media in browser with setting new data('url') etc. in div.shariff and re-initialize the Shariff object via this.shariff = new Shariff(this) or simiar. With this approach all links to all services were re-built and had correct params.

    Real world scenario: Client website uses this to share “deeplinks” to a slider by adding a hash to url and enhancing title with slide name etc. (and urlencode the fields).

    Also all kind of other custom params can be added to url with above approach.

    With 4.x this is much harder, you have to loop through all .shariff ul li a, parse their href and replace all possible fields url/u/.. and title/text/subject/description/.. by hand.

    Can you add an API for modification of url/title/etc. in client to 4.x?

    https://wordpress.org/plugins/shariff/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jan-Peter

    (@starguide)

    Hi Ov3rfly,

    could you provide a link to a page where this is being used (with the 3.x version for example) so I can get a better understanding of it?

    I’m sure we can figure something out.

    Cheers,
    JP

    Thread Starter Ov3rfly

    (@ov3rfly)

    Client does not allow a link, sorry.

    Currently a hash like http://example.com/page-url/#slide-5 is used to provide a deeplink which shows slide #5 at page-load. The hash is changed with each slider change.

    With 3.x we can (after hash-change) push the current url including the hash to all share-buttons by changing the data('url') once and re-initialize Shariff, similar to this pseudocode:

    $('.shariff').each(function() {
    	$(this).data('url', our_new_url_with_hash_and_urlencoded);
    	if (typeof Shariff === 'function') {
    		this.shariff = new Shariff(this);
    	}
    });

    This sets the correct url for all possible params to services like u=, url= etc., same for data('title') which after re-initialize is set to correct text=, title=, subject=, etc.

    With 4.x we have to find/replace all the different params one by one.

    Plugin Author Jan-Peter

    (@starguide)

    Got it. Stick with 3.4.2 for the moment. I’ll see how we can incorporate something like this in the next 4.x release.

    JP

    Thread Starter Ov3rfly

    (@ov3rfly)

    Thanks for quick feedback, we have updated already to 4.x.

    As temporary solution we added custom code which stores the initial href per service and after hash-change replaces/enhances in a .shariff ul li a loop all (currently known) fields if they are found (currently using http://stackoverflow.com/a/901144 as quick hack, there actually are more efficient solutions) based on the stored href.

    This will of course break if additional services / params are available after a plugin update but it works for now.

    Thread Starter Ov3rfly

    (@ov3rfly)

    For interested readers, above mentioned custom code:

    // for Shariff Wrapper 4.0.3+
    url = new_url_with_hash...
    title = new_title..
    media = new_media_url..
    $('.shariff ul li a').each(function() {
    	if (!$(this).data('full')) { // first call, store for later...
    		$(this).data('full', $(this).attr('href'));
    	}
    	full = $(this).data('full');
    	var href_without_hash = window.location.href.replace(window.location.hash, '');
    	// replace url, without reading params
    	full = full.replace(encodeURIComponent(href_without_hash), encodeURIComponent(url));
    	var whatsapp = (full.indexOf('whatsapp:')==0);
    	var threema = (full.indexOf('threema:')==0);
    	// replace title, are there other fields?
    	if (title) {
    		$.each(['text', 'subject', 'description', 'title'], function(index, val) {
    			var txt = getParameterByName(val, full);
    			if (whatsapp || threema) {
    				txt = txt.substring(0, txt.lastIndexOf(' '));
    			}
    			if (txt) {
    				full = full.replace(val + '=' + encodeURIComponent(txt), val + '=' + encodeURIComponent(title));
    				return false;	// we do only one replace, leave .each() now
    			}
    		});
    	}
    	// replace media, are there other fields?
    	if (media) {
    		$.each(['media'], function(index, val) {
    			var img = getParameterByName(val, full);
    			if (img) {
    				full = full.replace(val + '=' + encodeURIComponent(img), val + '=' + encodeURIComponent(media));
    				return false;	// we do only one replace, leave .each() now
    			}
    		});
    	}
    	$(this).attr('href', full);
    });

    Plugin Author Jan-Peter

    (@starguide)

    Thank you for sharing that with us!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Alter current page url/title/media in client for deeplink to slider or similar..’ is closed to new replies.