Add-on to exclude domains from right-click?

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

How about size="80" maxlength="100" :evilgrin: ?

It might be possible to add two text boxes to search for goods within a price range, but I don't want to upset Google (Andoooole?).
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
Hey Jude
5StarLounger
Posts: 1015
Joined: 24 Jan 2010, 15:45
Location: Ohio, U.S.A.

Re: Add-on to exclude domains from right-click?

Post by Hey Jude »

agibsonsw wrote: but I don't want to upset Google (Andoooole?).
Andrewg???

(are you an Andrew perchance?)
♫...Take a sad song and make it better . . .♫ Image

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15645
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Add-on to exclude domains from right-click?

Post by ChrisGreaves »

agibsonsw wrote:How about size="80" maxlength="100"
Now don't go Technical on me.
I Googled (using 'andy) for something about text box sizes, put it in and it seemed to work.
But I am not the javascript genius that you are, and I thought it wisest to seek confirmation from the maestro.
SIZE is presumably the displayed size of the textbox on the screen.
Does MAXLENGTH restrict the length of the text string in the etxtbox?
I saw something in passing about autosized text boxes, but I'm not very fond of those.

I will say at this point that i am amazed at what you've done. :hailpraise:
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

Yes, size is the display size of the box but maxlength is the number of characters you can type into it - the text will scroll until it reaches this number of characters.
I'm touched by all this praise but feel guilty now as the original piece of code wasn't mine. I modified it to exclude sites, allow apostrophes, etc, and then adapted it
to work from a web page. (I should track down the original author and give him a name check.)
I am a programmer but haven't studied JavaScript in any depth for quite a while. However, I'm quite keen at the moment and intend to modify the web page so that it can be
changed dynamically. That is, I will be able to type a domain and click an add button. The domain will then be added as a new checkbox, and the page will remember these
checkboxes the next time it's loaded. This means I won't have to keep editing the html source. Shouldn't be too tricky!
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

.. something along these lines:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Andy's Search Page</title>
<script type="text/javascript">
	function Go()
	{
		var sText;
		var sSites = "";
		sText = document.getElementById("searchText").value;
		if ( sText != "")
		{
			if (document.getElementById("ee").checked)
				sSites += " -site:experts-exchange.com";
			if (document.getElementById("br").checked)
				sSites += " -site:bigresource.com";
			if (document.getElementById("other").checked && document.getElementById("otherT").value != "")
				sSites += " -site:" + document.getElementById("otherT").value;
			// window.open("http://www.google.com/search?q=" + escape(sText).replace(/ /g,"+") + sSites);
			window.open("http://www.google.com/search?q=" + sText + sSites);
		}
		else
		{
			window.alert('You must enter something into the search box!');
			document.getElementById("searchText").focus();
		}
	}
	function AddCheck()
	{
		var sDomain =document.getElementById("otherT").value;
		if ( sDomain != "")
		{
			var newDiv = document.createElement("p");
			var newText = document.createTextNode(sDomain);
			var newChk = document.createElement("input");
			newChk.type = "checkbox";
			newChk.id = "other";
			newChk.checked = "checked";
			newChk.value="true";
			newDiv.appendChild(newChk);
			newDiv.appendChild(newText);
			document.body.appendChild(newDiv);
		}
	}
</script>
</head>
<body>
<h1>Andy's Search </h1>
<form id="myForm" action="">
<input type="text" id="searchText" size="80" maxlength="100" />
<button type="button" onclick="Go()">Go Search</button>
<p>Choose sites to exclude:</p>
<p><input type="text" id="otherT" />
<button type="button" onclick="AddCheck()">Add</button></p>
<p><input type="checkbox" id="ee" checked="checked" value="true" />Experts Exchange</p>
<p><input type="checkbox" id="br" checked="checked" value="true" />Big Resource</p>
</form>
</body>
</html>
Very much a work in progress, but I aim to modify it to loop through each added checkbox, excluding any checked ones from the search.

I should be able to store the list of domains in a cookie (so that they will be remembered next time). It shouldn't then be too much of a stretch to
selectively remove checkboxes, or even sort the list. Andy (or Andrew :grin: ).
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

Here's a link to the original site where I found the Bookmarklet:

http://www.smashingmagazine.com/2007/01 ... s/#Scene_1

This is a smashing magazine and has lot's of useful stuff about web design.

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

I have to mention this other site which is linked from Smashing Magazine:

http://slayeroffice.com/?c=/content/tools/suite.html

It has a 'favelet suite' which include a color list and mouseover DOM inspector. The color list shows a palette of all the colours used in the current page, and the DOM inspector lists styles as you point to page elements. I had to say WOW! when I saw this.

Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

I've got the version below working and wondered if someone might give it a trial?
It creates a cookie to remember the domains chosen. The cookie is set to last 20 days, but if you edit the html (search for 20) you can amend this number of days.

I've 'borrowed' some cookie code from http://techpatterns.com/downloads/javas ... ookies.php

When you type a domain you need to click Add, otherwise it will be ignored.

The thing that took so long was removing the check boxes from the page when you press Delete - apparently this is quite a common problem. (To Delete you need to type the Title that you gave the domain.)

Oh, and by the way, I haven't tested it in Internet Explorer! I thought I would delay that pleasure.

Thanks for any feedback. Andy.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Andy's Search Page</title>
<script type="text/javascript">

	function Go(frm)
	{
		var sText;
		var sSites = "";
		sText = document.getElementById("searchText").value;
		if ( sText != "" )
		{
			for (var i=0; i < frm.elements.length; i++)
				if ( frm.elements[i].name == 'checks' )
					if ( frm.elements[i].checked == true )
						sSites += ' -site:' + frm.elements[i].value;     		
			// window.open("http://www.google.com/search?q=" + escape(sText).replace(/ /g,"+") + sSites);
			window.open("http://www.google.com/search?q=" + sText + sSites);
		}
		else
		{
			window.alert('You must enter something into the search box!');
			document.getElementById("searchText").focus();
		}
	}
	function SetCookie( name, value, expires, path, domain, secure )
	{
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		/*
		If the expires variable is set, make the correct expires time.
		Expires is a number of days. To make it hours delete * 24,
		to make it minutes delete * 60 * 24.
		*/
		if ( expires )
		{
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		
		document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
	}
	
	// this fixes an issue with the old method, ambiguous values
	// with this test document.cookie.indexOf( name + "=" );
	function GetCookie( check_name ) 
	{
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
	
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );

			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
		}
	}
	
	// this deletes the cookie when called
	function DeleteCookie( name, path, domain ) 
	{
		if ( GetCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) +
				";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	function DeleteDomain( dTitle )
	{
		var cValue;
		if ( dTitle == "" || dTitle == null )
		{
			window.alert('Enter the correct Title to delete it.');
			document.getElementById("otherT").focus();
			return 0;
		}
		cValue = GetCookie(dTitle);
		if ( cValue )
		{	
			DeleteCookie(dTitle);
			var dadId = cValue.replace(/\./g,'') + '_dad';
			var dad = document.getElementById(dadId);
			var grandad = document.forms[0];
			var discard = grandad.removeChild(dad);    
		}
		else
		{
			window.alert("That title is incorrect, or it\'s deleted already.");
			document.getElementById("otherT").focus();
		}
	}
	function AddCheck(sDomain, sTitle, usingBox)	
	{
		if ( sDomain != "")
		{
			var newDiv = document.createElement("p");
			var newText = document.createTextNode((sTitle != "") ? sTitle : sDomain);
			var newChk = document.createElement("input");
			newChk.type = "checkbox";
			newChk.id = sDomain.replace(/\./g,'');
			newDiv.id = newChk.id + '_dad';
			newChk.name = "checks";
			newChk.checked = "checked";
			newChk.value = sDomain;
			newDiv.appendChild(newChk);
			newDiv.appendChild(newText);
			document.forms[0].appendChild(newDiv);
			SetCookie((sTitle != "") ? sTitle : sDomain,sDomain, 20);	// , '', '/', '', '' //
			// Cookie name/values will expire in 20 days - change this number to any other you fancy.
			if ( usingBox )
			{
				document.getElementById("otherD").value = '';
				document.getElementById("otherT").value = '';
			}
		}
		else if ( usingBox )
        {
            window.alert('Type a domain to exclude.');
            document.getElementById("otherD").focus();
        }
	}
	function GetAllDomains()
	{
		var allDoms = document.cookie.split( ';' );
		var pairs = '';
		var title = '';
		var dom = '';
		for ( i = 0; i < allDoms.length; i++ )
		{
			pairs = allDoms[i].split( '=' );
			// and trim left/right whitespace while we're at it
			title = pairs[0].replace(/^\s+|\s+$/g, '');
			dom = unescape( pairs[1].replace(/^\s+|\s+$/g, '') );
			AddCheck(dom,title,false);
		}
	}
	
</script>
</head>

<body onload="GetAllDomains();">
<h1>Andy's Search </h1>

<form id="myForm" action="">
<input type="text" id="searchText" size="80" maxlength="100" />
<button type="button" onclick="Go(document.forms[0]);">Go Search</button>
<p>Choose sites to exclude:</p>
<p>Domain&nbsp;<input type="text" id="otherD" />&nbsp;Title&nbsp;<input type="text" id="otherT" />&nbsp;&nbsp;&nbsp;
<button type="button" onclick="AddCheck(document.getElementById('otherD').value,document.getElementById('otherT').value,true);">
Add</button>&nbsp;&nbsp;
<button type="button" onclick="DeleteDomain(document.getElementById('otherT').value);">Delete</button></p>
</form>

</body>
</html>
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add-on to exclude domains from right-click?

Post by HansV »

That works very well, Andy! :thumbup:

Just a niggling request: would it be possible to initiate the search by pressing Enter after typing someting in the search box, just like in Google? Not a biggie if that is difficult.
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

Hi. Yes I'll give it a go.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
Hey Jude
5StarLounger
Posts: 1015
Joined: 24 Jan 2010, 15:45
Location: Ohio, U.S.A.

Re: Add-on to exclude domains from right-click?

Post by Hey Jude »

It worked for me too (Andrew)
's new search.png
:cheers: :clapping: :chocciebar:
You do not have the required permissions to view the files attached to this post.
♫...Take a sad song and make it better . . .♫ Image

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

Sometimes the simplest things can seem complex!
The easiest way to begin the search on pressing Enter is to change button="button" to button="submit" for the search button - but this clears the form data.
I tried a JavaScript event but this causes FireFox to treat it as a pop-up and a warning appears.
I found some quite complex code that stores the current values in a cookie and then re-reads the cookie to re-populate the controls.
Anyway, I sort-of guessed that if I used button="submit" and set the event 'onsubmit="return false;"' for the form, then this treats the submission as having failed and so, by default, it retains the previous values.

Well, this seems to work. The only slight issue is that the search will begin whenever you press Enter, regardless of where you are within the form. Let me know if this is annoying.

(I think Google et al use some quite complcated code to retain the search terms.)

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Andy's Search Page</title>
<script type="text/javascript">

	function Go(frm)
	{
		var sText;
		var sSites = "";
		sText = document.getElementById("searchText").value;
		if ( sText != "" )
		{
			for (var i=0; i < frm.elements.length; i++)
				if ( frm.elements[i].name == 'checks' )
					if ( frm.elements[i].checked == true )
						sSites += ' -site:' + frm.elements[i].value;     		
			// window.open("http://www.google.com/search?q=" + escape(sText).replace(/ /g,"+") + sSites);
			window.open("http://www.google.com/search?q=" + sText + sSites);
		}
		else
		{
			window.alert('You must enter something into the search box!');
			document.getElementById("searchText").focus();
		}
	}
	
	function SetCookie( name, value, expires, path, domain, secure )
	{
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		/*
		If the expires variable is set, make the correct expires time.
		Expires is a number of days. To make it hours delete * 24,
		to make it minutes delete * 60 * 24.
		*/
		if ( expires )
		{
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		
		document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
	}
	
	// this fixes an issue with the old method, ambiguous values
	// with this test document.cookie.indexOf( name + "=" );
	function GetCookie( check_name ) 
	{
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
	
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );

			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
		}
	}
	
	// this deletes the cookie when called
	function DeleteCookie( name, path, domain ) 
	{
		if ( GetCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) +
				";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	function DeleteDomain( dTitle )
	{
		var cValue;
		if ( dTitle == "" || dTitle == null )
		{
			window.alert('Enter the correct Title to delete it.');
			document.getElementById("otherT").focus();
			return 0;
		}
		cValue = GetCookie(dTitle);
		if ( cValue )
		{	
			DeleteCookie(dTitle);
			var dadId = cValue.replace(/\./g,'') + '_dad';
			var dad = document.getElementById(dadId);
			var grandad = document.forms[0];
			var discard = grandad.removeChild(dad);    
		}
		else
		{
			window.alert("That title is incorrect, or it\'s deleted already.");
			document.getElementById("otherT").focus();
		}
	}
	function AddCheck(sDomain, sTitle, usingBox)	
	{
		if ( sDomain != "")
		{
			var newDiv = document.createElement("p");
			var newText = document.createTextNode((sTitle != "") ? sTitle : sDomain);
			var newChk = document.createElement("input");
			newChk.type = "checkbox";
			newChk.id = sDomain.replace(/\./g,'');
			newDiv.id = newChk.id + '_dad';
			newChk.name = "checks";
			newChk.checked = "checked";
			newChk.value = sDomain;
			newDiv.appendChild(newChk);
			newDiv.appendChild(newText);
			document.forms[0].appendChild(newDiv);
			SetCookie((sTitle != "") ? sTitle : sDomain,sDomain, 20);	// , '', '/', '', '' //
			// Cookie name/values will expire in 20 days - change this number to any other you fancy.
			if ( usingBox )
			{
				document.getElementById("otherD").value = '';
				document.getElementById("otherT").value = '';
			}
		}
		else if ( usingBox )
        {
            window.alert('Type a domain to exclude.');
            document.getElementById("otherD").focus();
        }
	}
	function GetAllDomains()
	{
		var allDoms = document.cookie.split( ';' );
		var pairs = '';
		var title = '';
		var dom = '';
		for ( i = 0; i < allDoms.length; i++ )
		{
			pairs = allDoms[i].split( '=' );
			// and trim left/right whitespace while we're at it
			title = pairs[0].replace(/^\s+|\s+$/g, '');
			dom = unescape( pairs[1].replace(/^\s+|\s+$/g, '') );
			AddCheck(dom,title,false);
		}
	}
	
</script>
</head>

<body onload="GetAllDomains();">
<h1>Andy's Search </h1>

<form id="myForm" action="" onsubmit="return false;">
<input type="text" id="searchText" size="80" maxlength="100" />
<button type="submit" id="search" onclick="Go(document.forms[0]);">Go Search</button>
<p>Choose sites to exclude:</p>
<p>Domain&nbsp;<input type="text" id="otherD" />&nbsp;Title&nbsp;<input type="text" id="otherT" />&nbsp;&nbsp;&nbsp;
<button type="button" onclick="AddCheck(document.getElementById('otherD').value,document.getElementById('otherT').value,true);">
Add</button>&nbsp;&nbsp;
<button type="button" onclick="DeleteDomain(document.getElementById('otherT').value);">Delete</button></p>
</form>

</body>
</html>
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

And here's an exciting background image to use..
You do not have the required permissions to view the files attached to this post.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add-on to exclude domains from right-click?

Post by HansV »

agibsonsw wrote:The only slight issue is that the search will begin whenever you press Enter, regardless of where you are within the form. Let me know if this is annoying.
That's not a serious problem - I like being able to search by pressing Enter. Thanks!
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

I created another Bookmarklet and saved it in the same folder as my search page. I then dragged it to my Bookmarks toolbar, showing the text 'Exclude Site'. When I clicked it I could type a domain and when i opened my search page it was automatically added to it. I then got a little excited.

However, it then didn't work and started to behave erratically.

Here it is out of interest but I think I would advise you NOT to try it.

Code: Select all

<a href='javascript:q = prompt("Enter the site the exclude"); if (q!=null) document.cookie = escape(q).replace(/\./g,"") + "=" + escape(q); void 0'>Exclude Site</a>
The problem might be that it's creating a cookie from a local folder or that I haven't given it an expire time (which would be very messy). I don't think it copes well with the escaped periods (.) either.

So near and yet .. not quite. Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

For anyone who might still be interested in this topic :smile:

I wanted a bookmark to click on that would add a domain to my exclusion list automatically - I believe this goes back to the original post (all that time ago). My search page would then automatically add this as an excluded item. The intriguing thing was that it seemd to work the first couple of times?!

I spent ages trying to get this to work but (eventually) decided/realised that my code was correct, but it's not possible to create a cookie that would be stored in any other (cookie) location than that of the site currently being visited.

Code: Select all

javascript:q=prompt("Enter the site the exclude");
if(q!=null)document.cookie=escape(q).replace(/\./g,"")+"="+escape(q)+";
expires=01 Jan 2012 00:00:00 GMT;path='/C:/Users/Andrew/Documents/';domain='';";
void 0  // one of may attempts
I then tried to create a bookmarklet that would open my search page with the domain (to exclude) added to the URL as a parameter (with periods . replace with + signs). I would then be able to edit my search page to discover this parameter and add it to the excluded list. Again I spent ages fiddling around with back \ and forward slashes / to get it to open my page with the parameter added to the URL.

Even though you can choose File/Open within your browser to open a local file you cannot do this with JavaScript even, it seems, if the JavaScript originates from a locally stored file.

I'm loathe to admit defeat. On the positive side, perhaps this proves that JavaScript is a safe(-ish) technology?
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
HansV
Administrator
Posts: 78600
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Add-on to exclude domains from right-click?

Post by HansV »

I think this has to do with security indeed - you wouldn't want a script to mess with your hard disk!
Best wishes,
Hans

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

Quite right. The fact that everything was happening 'locally' made me think i might be able to do what I wanted.

I suppose if this were possible, a web site might be able to persuade the browser that it was stored locally and thus cause some damage.

There's nothing wrong with 'good=ol' Ctrl-C, Ctrl-V anyway! Andy.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15645
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: Add-on to exclude domains from right-click?

Post by ChrisGreaves »

agibsonsw wrote:For anyone who might still be interested in this topic :smile:
Andrew, I'm glad to see the :smile:.
I haven't been able to keep up with you, and my toolbar is gradually filling up with 'andy, 'andy1, 'andyTOO, etc.
I'll bet a pile of steaming cabbage rolls that there are a few others staring in amazement wondering what has been turned loose upon us.
I'm loathe to admit defeat.
Now you have to be kidding.
I went from a Petulant Query (11 Jan 2011, 09:32) to the gift of a working Proof-Of-Concept (11 Jan 2011, 14:08) in the space of 5 hours, and to a magnificent add-on (14 Jan 2011, 19:55) in the space of less than 4 days.
All I had to do, really, was keep my mouth shut (admittedly a difficult task at times), and mostly let others goad you into better things (14 Jan 2011, 18:57) on my behalf.

You have accomplished what at least one Parental Controls service (one of whose founders has been a good friend of mine for 20+ years) said in effect "Can't be done with our technology".

If this is defeat then I too am ready to give up. "Take me now, Lord!"

I offer you a resounding :clapping: :cheers: :chocciebar: :fanfare: :thankyou: :artist: :bananas: :basket: :bow: :bravo: :clever: :coffeetime: :cool: :doctor: :gent: :groovin: :hailpraise: :heavy: :love: :money: :pinkelephant: :ribbon: :rose: :sailing: :salute: :thumbup: :trophy: :wave: :wine: :exclamation:

And that's not even my :2cents: because this is a :free: board.
He who plants a seed, plants life.

User avatar
agibsonsw
SilverLounger
Posts: 2403
Joined: 05 Feb 2010, 22:21
Location: London ENGLAND

Re: Add-on to exclude domains from right-click?

Post by agibsonsw »

Chris, I was very much moved by your comments and am, frankly, lost for words. Thank you again - I've never seen so many smilies! They almost fell off the end of my screen.

I haven't quite finished though: The attached version can sort the list and delete all excluded domains. I've extended the expiration to 30 days (which can easily be changed within the HTML) and added some fonts. Edit these to use your favourite font - they're listed within the 'body' tag.

Again I would be grateful if someone could test it a little more. Thank you, Andy.
search1.png

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Andy's Search Page</title>
<script type="text/javascript">

	function Go(frm)
	{
		var sText;
		var sSites = "";
		sText = document.getElementById("searchText").value;
		if ( sText != "" )
		{
			for (var i=0; i < frm.elements.length; i++)
				if ( frm.elements[i].name == 'checks' )
					if ( frm.elements[i].checked == true )
						sSites += ' -site:' + frm.elements[i].value;     		
			// window.open("http://www.google.com/search?q=" + escape(sText).replace(/ /g,"+") + sSites);
			window.open("http://www.google.com/search?q=" + sText + sSites);
		}
		else
		{
			window.alert('You must enter something into the search box!');
			document.getElementById("searchText").focus();
		}
	}
	
	function SetCookie( name, value, expires, path, domain, secure )
	{
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		/*
		If the expires variable is set, make the correct expires time.
		Expires is a number of days. To make it hours delete * 24,
		to make it minutes delete * 60 * 24. */
		if ( expires )
		{
			expires = expires * 1000 * 60 * 60 * 24;
		}
		var expires_date = new Date( today.getTime() + (expires) );
		
		document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
	}
	
	// this fixes an issue with the old method, ambiguous values
	// with this test document.cookie.indexOf( name + "=" );
	function GetCookie( check_name ) 
	{
		// first we'll split this cookie up into name/value pairs
		// note: document.cookie only returns name=value, not the other components
		var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
	
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );

			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
		}
	}
	
	// this deletes the cookie when called
	function DeleteCookie( name, path, domain ) 
	{
		if ( GetCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") + ( ( domain ) ? ";domain=" + domain : "" ) +
				";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
	function DeleteDomain( dTitle )
	{
		var cValue;
		if ( dTitle == "" || dTitle == null )
		{
			window.alert('Enter the correct Title to delete it.');
			document.getElementById("otherT").focus();
			return 0;
		}
		cValue = GetCookie(dTitle);
		if ( cValue )
		{	
			DeleteCookie(dTitle);
			var dadId = cValue.replace(/\./g,'') + '_dad';
			var dad = document.getElementById(dadId);
			var grandad = document.forms[0];
			var discard = grandad.removeChild(dad);    
		}
		else
		{
			window.alert("That title is incorrect, or it\'s deleted already." + dTitle);
			document.getElementById("otherT").focus();
		}
	}
	function AddCheck(sDomain, sTitle, usingBox)	
	{
		if ( sDomain != "")
		{
			var newDiv = document.createElement("p");
			var newText = document.createTextNode((sTitle != "") ? sTitle : sDomain);
			var newChk = document.createElement("input");
			newChk.type = "checkbox";
			newChk.id = sDomain.replace(/\./g,'');
			newDiv.id = newChk.id + '_dad';
			newChk.name = "checks";
			newChk.checked = "checked";
			newChk.value = sDomain;
			newDiv.appendChild(newChk);
			newDiv.appendChild(newText);
			document.forms[0].appendChild(newDiv);
			SetCookie((sTitle != "") ? sTitle : sDomain,sDomain, 30);	// , '', '/', '', '' //
			// Cookie name/values will expire in 30 days - change this number to any other you fancy.
			if ( usingBox )
			{
				document.getElementById("otherD").value = '';
				document.getElementById("otherT").value = '';
			}
		}
		else if ( usingBox )
        {
            window.alert('Type a domain to exclude.');
            document.getElementById("otherD").focus();
        }
	}
	function GetAllDomains()
	{
		var allDoms = document.cookie.split( ';' );
		var pairs = '';
		var title = '';
		var dom = '';
		for ( var i = 0; i < allDoms.length; i++ )
		{
			pairs = allDoms[i].split( '=' );
			// and trim left/right whitespace while we're at it
			title = pairs[0].replace(/^\s+|\s+$/g, '');
			dom = unescape( pairs[1].replace(/^\s+|\s+$/g, '') );
			AddCheck(dom,title,false);
		}
	}
	function DelAllDomains(ask)
	{
		if ( document.cookie.length > 0 )
		{
			if ( ask ) 
			{
				var ans = confirm("Delete all domains?");
				if ( !ans ) return 0;
			}
			var allDoms = document.cookie.split( ';' );
			var pairs = '';
			var title = '';
			for ( var i = allDoms.length-1; i >=0 ; i-- )
			{
				pairs = allDoms[i].split( '=' );
				// and trim left/right whitespace while we're at it
				title = pairs[0].replace(/^\s+|\s+$/g, '');
				DeleteDomain(title);
			}
		}
		else 
			window.alert('Nothing to delete.');
	}
	function SortDomains()
	{
		if ( document.cookie.length > 0 )
		{
			var allDoms = document.cookie.replace(/(;)\s+/g, '$1').split(';').sort();
			DelAllDomains(false);
			var pairs = '';
			var title = '';
			var dom = '';
			for ( var i = 0; i < allDoms.length; i++ )
			{
				pairs = allDoms[i].split( '=' );
				// and trim left/right whitespace while we're at it
				title = pairs[0].replace(/^\s+|\s+$/g, '');
				dom = unescape( pairs[1].replace(/^\s+|\s+$/g, '') );
				AddCheck(dom,title,false);
			}
		}
		else
			window.alert('Nothing to sort.');
	}
</script>
</head>

<body style="font-family:Trebuchet MS, Georgia, Lucida;" onload="GetAllDomains();">
<h1>Andy's Search </h1>

<form id="myForm" action="" onsubmit="return false;">
<input type="text" id="searchText" size="80" maxlength="100" />
<button type="submit" id="search" onclick="Go(document.forms[0]);">Go Search</button>
<p>Choose sites to exclude:</p>
<p>Domain&nbsp;<input type="text" id="otherD" />&nbsp;Title&nbsp;<input type="text" id="otherT" />&nbsp;&nbsp;&nbsp;
<button type="button" onclick="AddCheck(document.getElementById('otherD').value,document.getElementById('otherT').value,true);">
Add</button>&nbsp;&nbsp;
<button type="button" onclick="DeleteDomain(document.getElementById('otherT').value);">Delete</button></p>
<button type="button" onclick="DelAllDomains(true);">Delete All</button></p>
<button type="button" onclick="SortDomains();">Sort</button></p>
</form>

</body>
</html>
You do not have the required permissions to view the files attached to this post.
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.