Google Quoter Button that needs to be fixed

Forums » General Opera topics » User JavaScript

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

19. May 2010, 17:17:47

combomax

Posts: 83

Google Quoter Button that needs to be fixed

I have a userjs button for google search that put quotes around sentences. It was working fine until google change its page recently. I have other button that still working fine. How to fix the button below

(Google Quoter Button) needs to be fixed:

// ==UserScript==
// @name           Google Quote-Adder
// @description    Adds a button that puts quotemarks around your Google search string. The semicolon key also works.
// @include        http://*google.*/*
// @exclude        http://maps.google.com/*
// @exclude        http://*.google.com/preferences/*
// @exclude        http://translate.google.com/*
// ==/UserScript==

var SearchBoxes, AQButton;
var semicolon = 59;
var enter = 13;

function $x(p, context) {
  if (!context) context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, 
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  return arr;
}

function insertAfter(newNode, node) {
  return node.parentNode.insertBefore(newNode, node.nextSibling);
}

function last (arr) {
  return arr[arr.length-1];
}

function stripSemis (a) {
  a.value = a.value.replace(/;/g,"");
}

function makeAQButton () {
  AQButton = document.createElement("input");
  AQButton.type="button";
  AQButton.value="Quote";
}

function addQuotes (b) {
  s = b.selectionStart;
  e = b.selectionEnd;
  if (s != e) {
    b.value = b.value.substr(0,s) + b.value.substr(e);
  }
  c = b.value.lastIndexOf('"',s);
  while ((b.value[c+1] == ' ') || (b.value[c+1] == ';'))  
    {c += 1;}
  while ((b.value[s-1] == ' ') || (b.value[s-1] == ';'))  
    {s += -1;}
  b.value = b.value.substr(0,c+1) + '"' + b.value.slice(c+1,s) + '"' + b.value.slice(s);
  b.focus();
  b.selectionStart = s+2;   // +2 because we added 2 quotemarks
  b.selectionEnd = s+2;
}

function setup(a) {
  makeAQButton ();
  var buttons = $x(".//input[@type='submit']",a.parentNode);
  insertAfter(AQButton,last(buttons));
  AQButton.addEventListener ("click",function(){addQuotes(a)},true);
  a.addEventListener ("keypress",function(evt) {
                        if (evt.charCode == semicolon) {addQuotes(a)}
                      },true);
  a.addEventListener ("keydown",function(evt) {
                        if (evt.which == enter) {stripSemis(a)}
                      },true);
  for (i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener ("click",function(){stripSemis(a)},true);
  }
}

SearchBoxes = $x('//input[contains(@name, "q")]');
setup (SearchBoxes[0]);
if (SearchBoxes.length > 1) {         // deal with search box at bottom of page
  setup (last(SearchBoxes));
}



Other button (Google Clear Button) code works fine:

// ==UserScript==
// @name          Google Clear Button
// @namespace     http://eric.jain.name/2007/03/13/google-clear-button/
// @description   adds a clear button to search forms at Google
// @include       http://*.google.*/*
// @exclude        http://translate.google.com/*
// ==/UserScript==

addClearButton('gs', 'btnG') || // Web, Images, Blog
addClearButton('gs', 'qt_s') || // Groups
addClearButton('f', 'button-search') || // Video
addClearButton('f', 'btnG') || // Books, Patents, Scholar, Products
addClearButton('s', 'btnS') || // Calendar
addClearButton('search-form', 'search-submit') || // Reader
addClearButton('q_form', 'q_sub'); // Maps
// TODO: Finance, Mail, Photos

function addClearButton(formId, buttonId) {
	var form = document.forms.namedItem(formId, buttonId);
	if (form) {
		var target = form.elements.namedItem(buttonId);
		if (target) {
			var button = document.createElement('input');
			button.setAttribute('type', 'button');
			button.setAttribute('value', 'Clear');
			button.setAttribute('style', 'margin-left:0.25em');
			button.setAttribute('onclick', "var el=document.forms['" + formId + "'].q;el.value='';el.focus()");
			target.parentNode.insertBefore(button, target.nextSibling);
			return true;
		}
	}
	return false;
}

20. May 2010, 08:39:51

splondike

Posts: 528

Here's a fixed script, I changed makeAQButton and setup, removed stripSemis and googlified the button bigsmile. Also, the semicolon press wouldn't work in opera, so fixed that too:
// ==UserScript==
// @name           Google Quote-Adder
// @description    Adds a button that puts quotemarks around your Google search string. The semicolon key also works.
// @include        http://*google.*/*
// @exclude        http://maps.google.com/*
// @exclude        http://*.google.com/preferences/*
// @exclude        http://translate.google.com/*
// ==/UserScript==

var SearchBoxes, AQButton;
var semicolon = 59;
var enter = 13;

function $x(p, context) {
  if (!context) context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, 
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  return arr;
}

function insertAfter(newNode, node) {
  return node.parentNode.insertBefore(newNode, node.nextSibling);
}

function last (arr) {
  return arr[arr.length-1];
}

function makeAQButton () {
  AQButton = $x('//input[@name="btnG"]/../..')[0].cloneNode(true);
  var btn = AQButton.getElementsByTagName("input")[0];
  btn.type="button";
  btn.value="Quote";
}

function addQuotes (b) {
  s = b.selectionStart;
  e = b.selectionEnd;
  if (s != e) {
    b.value = b.value.substr(0,s) + b.value.substr(e);
  }
  c = b.value.lastIndexOf('"',s);
  while ((b.value[c+1] == ' ') || (b.value[c+1] == ';'))  
    {c += 1;}
  while ((b.value[s-1] == ' ') || (b.value[s-1] == ';'))  
    {s += -1;}
  b.value = b.value.substr(0,c+1) + '"' + b.value.slice(c+1,s) + '"' + b.value.slice(s);
  b.focus();
  b.selectionStart = s+2;   // +2 because we added 2 quotemarks
  b.selectionEnd = s+2;
}

function setup(a) {
  makeAQButton ();
  var buttons = $x('ancestor::td[@class="lst-td"]/following-sibling::td', a);
  insertAfter(AQButton, last(buttons));
  AQButton.addEventListener ("click",function(){addQuotes(a)},true);
  a.addEventListener ("keypress",function(evt) {
	if (evt.keyCode == semicolon) {
	  addQuotes(a);
	  evt.preventDefault();
	}
  },true);
}

SearchBoxes = $x('//input[contains(@name, "q")]');
setup (SearchBoxes[0]);
if (SearchBoxes.length > 1) {         // deal with search box at bottom of page
  setup (last(SearchBoxes));
}

20. May 2010, 14:21:36

combomax

Posts: 83

The button appeared and functional however few conflicts with my other two buttons. My "clear button" is now duplicated and other button lost after I added your fixed button. All three button's script downloadable here could you test it?

http://hotfile.com/dl/43831745/2a23fcb/userjss.zip.html


21. May 2010, 06:55:35

splondike

Posts: 528

You should upload files to your my.opera account, or at least a less annoying host! Here you go:
// ==UserScript==
// @name           Google Quote-Adder
// @description    Adds a button that puts quotemarks around your Google search string. The semicolon key also works.
// @include        http://*google.*/*
// @exclude        http://maps.google.com/*
// @exclude        http://*.google.com/preferences/*
// @exclude        http://translate.google.com/*
// ==/UserScript==

var SearchBoxes, AQButton;
var semicolon = 59;
var enter = 13;

function $x(p, context) {
  if (!context) context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, 
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  return arr;
}

function insertAfter(newNode, node) {
  return node.parentNode.insertBefore(newNode, node.nextSibling);
}

function last (arr) {
  return arr[arr.length-1];
}

function makeAQButton () {
  AQButton = $x('//input[@name="btnG"]/../..')[1].cloneNode(true);
  var btn = AQButton.getElementsByTagName("input")[0];
  btn.name="quote-button";
  btn.type="button";
  btn.value="Quote";
}

function addQuotes (b) {
  s = b.selectionStart;
  e = b.selectionEnd;
  if (s != e) {
    b.value = b.value.substr(0,s) + b.value.substr(e);
  }
  c = b.value.lastIndexOf('"',s);
  while ((b.value[c+1] == ' ') || (b.value[c+1] == ';'))  
    {c += 1;}
  while ((b.value[s-1] == ' ') || (b.value[s-1] == ';'))  
    {s += -1;}
  b.value = b.value.substr(0,c+1) + '"' + b.value.slice(c+1,s) + '"' + b.value.slice(s);
  b.focus();
  b.selectionStart = s+2;   // +2 because we added 2 quotemarks
  b.selectionEnd = s+2;
}

function setup(a) {
  makeAQButton ();
  var buttons = $x('ancestor::td[@class="lst-td"]/following-sibling::td', a);
  insertAfter(AQButton, last(buttons));
  AQButton.addEventListener ("click",function(){addQuotes(a)},true);
  a.addEventListener ("keypress",function(evt) {
	if (evt.keyCode == semicolon) {
	  addQuotes(a);
	  evt.preventDefault();
	}
  },true);
}

SearchBoxes = $x('//input[contains(@name, "q")]');
setup (SearchBoxes[0]);
if (SearchBoxes.length > 1) {         // deal with search box at bottom of page
  setup (last(SearchBoxes));
}

23. May 2010, 06:49:27

splondike

Posts: 528

Alright, try this one. Don't name it *.user.js, "Google Quoter Button.js" would be fine:
// ==UserScript==
// @name           Google Quote-Adder
// @description    Adds a button that puts quotemarks around your Google search string. The semicolon key also works.
// @include        http://*google.*/*
// @exclude        http://maps.google.com/*
// @exclude        http://*.google.com/preferences/*
// @exclude        http://translate.google.com/*
// ==/UserScript==

(function(){
var SearchBoxes, AQButton;
var semicolon = 59;
var enter = 13;

function $x(p, context) {
  if (!context) context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, 
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  return arr;
}

function insertAfter(newNode, node) {
  return node.parentNode.insertBefore(newNode, node.nextSibling);
}

function last (arr) {
  return arr[arr.length-1];
}

function makeAQButton () {
  AQButton = last($x('//input[@name="btnG"]/../..')).cloneNode(true);
  var btn = AQButton.getElementsByTagName("input")[0];
  btn.name="quote-button";
  btn.type="button";
  btn.value="Quote";
}

function addQuotes (b) {
  s = b.selectionStart;
  e = b.selectionEnd;
  if (s != e) {
    b.value = b.value.substr(0,s) + b.value.substr(e);
  }
  c = b.value.lastIndexOf('"',s);
  while ((b.value[c+1] == ' ') || (b.value[c+1] == ';'))  
    {c += 1;}
  while ((b.value[s-1] == ' ') || (b.value[s-1] == ';'))  
    {s += -1;}
  b.value = b.value.substr(0,c+1) + '"' + b.value.slice(c+1,s) + '"' + b.value.slice(s);
  b.focus();
  b.selectionStart = s+2;   // +2 because we added 2 quotemarks
  b.selectionEnd = s+2;
}

function setup(a) {
  makeAQButton ();
  if(document.location.pathname == "/"){
	  var buttons = $x('../../span[@class="ds"]', a);
  }else{
	  var buttons = $x('ancestor::td[@class="lst-td"]/following-sibling::td', a);
  }
  insertAfter(AQButton, last(buttons));
  AQButton.addEventListener ("click",function(){addQuotes(a)},true);
  a.addEventListener ("keypress",function(evt) {
	if (evt.keyCode == semicolon) {
	  addQuotes(a);
	  evt.preventDefault();
	}
  },true);
}

addEventListener("DOMContentLoaded", function(){
SearchBoxes = $x('//input[@name="q"]');
setup (SearchBoxes[0]);
if (SearchBoxes.length > 1) {         // deal with search box at bottom of page
  setup (last(SearchBoxes));
}
}, false);
})();

23. May 2010, 12:08:53 (edited)

combomax

Posts: 83

Now button appears in every page but not functional in any page.

23. May 2010, 13:14:53

splondike

Posts: 528

Odd, it works for me on all the pages. Could you list the other user javascripts you have installed? Also, could you post any javascript errors you get when clicking the button. To do this go to Tools -> Advanced -> Error Console, then change the first dropdown from "All" to "JavaScript". Post any messages you get here. I guess the names will be different if your interface is in Turkish, but hopefully you'll find it.

24. May 2010, 21:20:35

combomax

Posts: 83

Could you fix the conflict (via either script) they're both life-saver scripts for me.

25. May 2010, 06:18:58

splondike

Posts: 528

Yep. This is a replacement for your multi engine script. The problem is replacing content with .innerHTML wipes any event listeners:
// ==UserScript==
// @include        http://www.google.com.tr/*
// @include        http://images.google.com.tr/*
	// ==/UserScript==


engines  = new Object();


//CONFIG//

/*/
 You can add more search-engines by copying the line below, 
 pasting it under the allready existing lines, and editing it:
 
 engines['QUICKNAME'] = "NAME; URL";
 
 You have to split the NAME and the URL by using '; '.
 In the URL you can use %KEYWORDS% instead of the keys searched for. As you see below.
/*/


engines['discogs'] = "discogs; http://www.discogs.com/search?q=%KEYWORDS%&type=artists&btn=Search";
engines['blog'] = "blog; http://blogsearch.google.com/blogsearch?hl=tr&ie=UTF-8&q=%KEYWORDS%&btnG=Bloglarda+Ara&lr=";
engines['yahoo'] = "Yahoo; http://search.yahoo.com/search?p=%KEYWORDS%";
engines['Bing']  = "Windows Live; http://www.bing.com/search?q=%KEYWORDS%&go=&form=QBRE&filt=all";
engines['wiki']  = "Wikipedia; http://en.wikipedia.org/wiki/Special:Search?search=%KEYWORDS%&fulltext=Search";
engines['ytube'] = "YouTube; http://www.youtube.com/results?search_query=%KEYWORDS%";
engines['freedb'] = "freedb; http://www.freedb.org/freedb_search.php?page=1&words=%KEYWORDS%&allfields=NO&fields%5B%5D=artist&fields%5B%5D=title&allcats=YES&grouping=none";
engines['seekacover'] = "seekacover; http://www.seekacover.com/cd/%KEYWORDS%";
engines['brothersoft'] = "brothersoft; http://search.brothersoft.com/?act=search.index&keyword=%KEYWORDS%&stype=windows&sug_q_pre=&sug_q_sel=";
engines['amazon'] = "amazon; http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dpopular&field-keywords=%KEYWORDS%&x=0&y=0";
engines['yahoo images'] = "yahoo images; http://images.search.yahoo.com/search/images;_ylt=A0WTefMchtBLuEAAnl.JzbkF?p=%KEYWORDS%&fr=&ei=utf-8&x=wrt&y=Search";
engines['picsearch'] = "picsearch; http://www.picsearch.com/search.cgi?q=%KEYWORDS%&cols=&thumbs=&t=EhjJBdeK%252BZhzL9K%252FXDTGceOazBErsD%252FyA7bkMP7IP6s%253D";
engines['flickr'] = "flickr; http://www.flickr.com/search/?w=all&q=%KEYWORDS%&m=text";




//CONFIG//


function search(engine){
  var q         = document.getElementsByName('q');
  if(engine != "" && q){
    var q       = escape(q[0].value);

    var engine2 = engines[engine].split('; ');
    var url     = engine2[1];
    var url     = url.replace("%KEYWORDS%",q);

    document.location.href = url;
  }
}

(function(){
  var select   = '&nbsp;<select id="engines">\n';

  
  for (engine in engines){
    var engine2 = engines[engine].split('; ');
    var name    = engine2[0];
    var url     = engine2[1];
  
    var select = select+'<option value="'+engine+'">'+name+'</option>\n';
  }
  
  var select   = select+'</select>';
  
  
  var q = document.getElementsByName('q');
  if(document.getElementById('ap')){
    with(document.getElementById('ap').parentNode){ innerHTML = '<div style="float:left;">'+select+'</div>' + innerHTML; }
  }else if(q){
    var dummy = document.createElement("span");
    dummy.innerHTML = select;
    q[0].parentNode.appendChild(dummy);
  }
  
  
  if(document.getElementById('engines')){
    document.getElementById('engines').addEventListener('change',  function() { search(this.value); }, 'false');
  }
}());

25. May 2010, 20:09:48

combomax

Posts: 83

Thanks splondike for revived that great script.

27. May 2010, 23:38:11

combomax

Posts: 83

Google changed something so button disappeared, could you fix the script again?

28. May 2010, 03:34:29

splondike

Posts: 528

Yup:
// ==UserScript==
// @name           Google Quote-Adder
// @description    Adds a button that puts quotemarks around your Google search string. The semicolon key also works.
// @include        http://*google.*/*
// @exclude        http://maps.google.com/*
// @exclude        http://*.google.com/preferences/*
// @exclude        http://translate.google.com/*
// ==/UserScript==

(function(){
var SearchBoxes, AQButton;
var semicolon = 59;
var enter = 13;

function $x(p, context) {
  if (!context) context = document;
  var i, arr = [], xpr = document.evaluate(p, context, null, 
                            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  for (i = 0; item = xpr.snapshotItem(i); i++) arr.push(item);
  return arr;
}

function insertAfter(newNode, node) {
  return node.parentNode.insertBefore(newNode, node.nextSibling);
}

function last (arr) {
  return arr[arr.length-1];
}

function makeAQButton () {
  AQButton = last($x('//input[@name="btnG"]/../..')).cloneNode(true);
  var btn = AQButton.getElementsByTagName("input")[0];
  btn.name="quote-button";
  btn.type="button";
  btn.value="Quote";
}

function addQuotes (b) {
  s = b.selectionStart;
  e = b.selectionEnd;
  if (s != e) {
    b.value = b.value.substr(0,s) + b.value.substr(e);
  }
  c = b.value.lastIndexOf('"',s);
  while ((b.value[c+1] == ' ') || (b.value[c+1] == ';'))  
    {c += 1;}
  while ((b.value[s-1] == ' ') || (b.value[s-1] == ';'))  
    {s += -1;}
  b.value = b.value.substr(0,c+1) + '"' + b.value.slice(c+1,s) + '"' + b.value.slice(s);
  b.focus();
  b.selectionStart = s+2;   // +2 because we added 2 quotemarks
  b.selectionEnd = s+2;
}

function setup(a) {
  makeAQButton ();
  if(document.location.pathname == "/"){
	  var buttons = $x('../../span[@class="ds"]', a);
  }else{
	  var buttons = $x('ancestor::td[contains(@class, "lst-td")]/following-sibling::td', a);
  }
  insertAfter(AQButton, last(buttons));
  AQButton.addEventListener ("click",function(){addQuotes(a)},true);
  a.addEventListener ("keypress",function(evt) {
	if (evt.keyCode == semicolon) {
	  addQuotes(a);
	  evt.preventDefault();
	}
  },true);
}

addEventListener("DOMContentLoaded", function(){
SearchBoxes = $x('//input[@name="q"]');
setup (SearchBoxes[0]);
if (SearchBoxes.length > 1) {         // deal with search box at bottom of page
  setup (last(SearchBoxes));
}
}, false);
})();

28. May 2010, 13:25:49

combomax

Posts: 83

"Thank you" splondike. wink

9. November 2012, 15:30:24

tunayx

Posts: 183

After recent changes in Google this great script stop appearing again, can someone help?

Forums » General Opera topics » User JavaScript