www.webdeveloper.com
Results 1 to 6 of 6

Thread: Bookmarklet: adding a Prompt to confirm text

  1. #1
    Join Date
    Dec 2008
    Posts
    4

    Bookmarklet: adding a Prompt to confirm text

    I'm using a bookmarklet script that publish the name and link of the website im browsing in plurk.com but that script will not allow me to edit it before is publish. Also it handles youtube links and flickr links but in their own way... im concerned in the function plurkText

    Bookmarklet code that I'm using:
    Code:
    javascript: function plurk()
    {
    	if (checkUrl()) {
    		return true;
    	}
    	
    	if (checkSelection()) {
    		return true;
    	}
    	
    	if (checkTitle()) {
    		return true;
    	}
    	
    	return false;	
    }
    
    /* Check the url for patterns */
    function checkUrl()
    {
    	var matches;
    	
    	if (/^http:\/\/[a-z]+\.youtube\.com\/watch?/.test(window.location.href)) {
    		var videoID = parameters()['v'];
    		
    		if (videoID) {
    			plurkYoutubeVideo(videoID);
    			
    			return true;
    		}
    	}
    	
    	/* test flickr */
    	matches = /^http:\/\/(www\.)?flickr\.com\/photos\/([^\/]+)\/([0-9]+)/.exec(window.location.href);
    	if (matches) {
    		var username = matches[2];
    		var imageID = matches[3];
    		
    		plurkFlickrImage(username, imageID);
    		
    		return true;
    	}
    	
    	return false;
    }
    
    function checkSelection()
    {
    	var selection = window.getSelection();
    	
    	var selectionText = selection.toString();
    	if (selectionText.length > 0) {
    		plurkText(selectionText);
    		
    		return true;
    	}
    }
    
    function checkTitle()
    {
    	var headTag = document.getElementsByTagName('head')[0];
    	if (!headTag) {
    		return false;
    	}
    	
    	var titleTag = headTag.getElementsByTagName('title')[0];
    	if (!titleTag) {
    		return false;
    	}
    	
    	var title = titleTag.textContent;
    	
    	if (title.length > 0) {
    		plurkText(title);
    		
    		return true;
    	}
    }
    
    function plurkYoutubeVideo(videoID)
    {
    	/* TODO: fixa internationella sidor */
    	var videoUrl = "http://www.youtube.com/watch?v=" + videoID;
    	
    	/* create plurk */
    	var plurk = videoUrl;	
    	
    	/* send the plurk */
    	sendPlurk(plurk, {});
    }
    
    function plurkFlickrImage(username, imageID)
    {
    	var photoUrl = "http://flickr.com/photos/" + username + "/" + imageID;
    	
    	var plurk = photoUrl;
    	
    	/* send the plurk */
    	sendPlurk(plurk, {});	
    }
    
    function plurkText(text)
    {
    	var url = window.location;
    	
    	/* check title */
    	if (text.length == 0) {
    		return false;
    	}
    	
    	/* create plurk */
    	var plurk = url + " (" + text + ")";
    	
    	/* send the plurk */
    	sendPlurk(plurk, {});
    }
    
    
    function sendPlurk(message, options)
    {
    	var now = new Date();
    	
    	var parameters = {
    		content: message + " ",
    		posted: now.plurkify(),
    		no_comments: Number(options['disableComments'] || false),
    		lang: (options['language'] || 'en'),
    		qualifier: (options['verb'] || 'shares')
    		
    	};
    	
    	var queryComponents = new Array();
    	
    	for (var parameterName in parameters) {
    		var parameterValue = parameters[parameterName];
    		
    		if (parameterValue !== null) {		
    			queryComponents.push(parameterName + "=" + encodeURIComponent(parameterValue));
    		}
    	}
    	
    	var query = queryComponents.join("&");
    		
    	/* send query */
    	window.location = "http://www.plurk.com/addPlurk?" + query;
    }
    
    Date.prototype.plurkify = function() {
    	return (this.getFullYear() + "-" + this.getDate() + "-" + (this.getMonth() + 1) + "T" + this.getHours().width(2) + ":" + this.getMinutes().width(2) + ":" + this.getSeconds().width(2));
    }
    
    Number.prototype.width = function(width) {
    	var stringRepresentation = this.toString();
    	
    	/* split the string */
    	var matches = /^[+-]?([0-9]*)(\.[0-9]*)?$/.exec(stringRepresentation);
    	
    	/* calcultate the number of zeros to add */
    	var zerosNeeded = width - matches[1].length;
    	
    	if (zerosNeeded > 0) {
    		/* add zeros before the string */
    		stringRepresentation = "0".repeat(zerosNeeded) + stringRepresentation;
    	}
    	
    	return stringRepresentation;
    }
    
    Number.prototype.pad = function() {
    	var stringRepresentation = this.toString();
    	
    	if (stringRepresentation.length == 1) {
    		stringRepresentation = '0' + stringRepresentation;
    	}
    	
    	return stringRepresentation;
    }
    
    String.prototype.repeat = function(times) {
    	var result = new String();
    	
    	for (var count = 0; count < times; ++count) {
    		result += this;
    	}
    	
    	return result;
    }
    
    function parameters()
    {
    	var query = window.location.search.slice(1);
    	var parameterStrings = query.split('&');
    	var parameters = {};
    	
    	for (var current = 0; current < parameterStrings.length; ++current) {
    		var parameterString = parameterStrings[current];
    		
    		var matches = /^([^=]+)=(.*)$/.exec(parameterString);
    		if (matches) {
    			parameters[matches[1]] = decodeURIComponent(matches[2]);
    		}
    	}
    		
    	return parameters
    }
    
    /* create and post plurk */
    plurk();
    Is there a way to be able to edit the name of the link before is sent to plurk?
    This is the section that need to be edit... that's what I think...

    Code:
    function plurkText(text)
    {
    	var url = window.location;
    	
    	/* check title */
    	if (text.length == 0) {
    		return false;
    	}
    	
    	/* create plurk */
    	var plurk = url + " (" + text + ")";
    	
    	/* send the plurk */
    	sendPlurk(plurk, {});
    }

  2. #2
    Join Date
    Oct 2008
    Posts
    28
    Is there a way to be able to edit the name of the link before is sent to plurk?
    This is the section that need to be edit... that's what I think...
    Edit the link in what way? The function you have there takes the current location and appends some text onto it before sending it.

  3. #3
    Join Date
    Dec 2008
    Posts
    4
    Quote Originally Posted by animalj7 View Post
    Edit the link in what way? The function you have there takes the current location and appends some text onto it before sending it.
    let me explain:

    that "some text" is the TITLE of the link but what I want is to edit that Title before is sent.

  4. #4
    Join Date
    Dec 2008
    Posts
    4
    I forgot to add the function that takes the title.. is in the main JS up in the first post... but I'm posting it again here just in case...

    Code:
    function checkTitle()
    {
    	var headTag = document.getElementsByTagName('head')[0];
    	if (!headTag) {
    		return false;
    	}
    	
    	var titleTag = headTag.getElementsByTagName('title')[0];
    	if (!titleTag) {
    		return false;
    	}
    	
    	var title = titleTag.textContent;
    	
    	if (title.length > 0) {
    		plurkText(title);
    		
    		return true;
    	}
    }
    so please... I still need help with this script

  5. #5
    Join Date
    Dec 2008
    Posts
    22
    the prompt() function would be very helpful as long as you're not using IE

  6. #6
    Join Date
    Dec 2008
    Posts
    4
    Quote Originally Posted by Sephr View Post
    the prompt() function would be very helpful as long as you're not using IE
    yeah... I'm a firefox user... also I don't think there are many users using this bookmarklet in IE.. and if they are... this will be a reason to change/convert them...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles