🏠 Home 

Greasy Fork is available in English.

Exclude term on Google (Firefox only)

Easily exclude terms from your query using the right-click context menu. v0.9.0 2015-12-20


安装此脚本?
  1. // ==UserScript==
  2. // @name Exclude term on Google (Firefox only)
  3. // @author Jefferson "jscher2000" Scher
  4. // @namespace JeffersonScher
  5. // @copyright Copyright 2015 Jefferson Scher
  6. // @license BSD 3-clause
  7. // @description Easily exclude terms from your query using the right-click context menu. v0.9.0 2015-12-20
  8. // @include http*://www.google.*/*
  9. // @include http*://encrypted.google.*/*
  10. // @version 0.9.0
  11. // @grant none
  12. // ==/UserScript==
  13. // DISCLAIMER: Use at your own risk. Functionality and harmlessness cannot be guaranteed.
  14. var ETGtext = "";
  15. // Context menu options -- Firefox only; do not replace any existing menu!
  16. if (!document.body.hasAttribute("contextmenu") && "contextMenu" in document.documentElement){
  17. var cmenu = document.createElement("menu");
  18. cmenu.id = "ETGcontext";
  19. cmenu.setAttribute("type", "context");
  20. cmenu.innerHTML = '<menuitem id="ETGexTerm" label="Exclude term(s)"></menuitem>' +
  21. '<menuitem id="ETGexPhrase" label="Exclude phrase" disable="disable"></menuitem>';
  22. document.body.appendChild(cmenu);
  23. document.getElementById("ETGexTerm").addEventListener("click", ETG_doExclude, false);
  24. document.getElementById("ETGexPhrase").addEventListener("click", ETG_doExclude, false);
  25. // attach menu and create event for filtering
  26. document.body.setAttribute("contextmenu", "ETGcontext");
  27. document.body.addEventListener("contextmenu", ETG_cmenuFilter, false);
  28. }
  29. function ETG_cmenuFilter(e){
  30. var s = window.getSelection();
  31. if (s.isCollapsed){
  32. ETGtext = ETG_getWord(e.rangeParent, e.rangeOffset);
  33. document.getElementById("ETGexPhrase").setAttribute("disabled","disabled");
  34. } else {
  35. ETGtext = s.getRangeAt(0).toString().trim();
  36. if (ETGtext.indexOf(" ") > -1) document.getElementById("ETGexPhrase").removeAttribute("disabled");
  37. }
  38. }
  39. function ETG_getWord(rnode, rstart){
  40. var txtFull = rnode.textContent;
  41. var startPos = rstart, endOffset = 1;
  42. // Expand to the left of the click point
  43. while(startPos > 0 && txtFull.substr(startPos, endOffset).indexOf(' ') != 0) {
  44. startPos = startPos - 1;
  45. endOffset += 1;
  46. }
  47. if (txtFull.substr(startPos, endOffset).indexOf(' ') == 0) {
  48. startPos = startPos + 1;
  49. endOffset = endOffset - 1;
  50. }
  51. // Expand to the right of the click point
  52. while(startPos + endOffset + 1 < txtFull.length && txtFull.substr(startPos, endOffset).indexOf(' ') == -1) {
  53. endOffset = endOffset + 1;
  54. }
  55. return txtFull.substr(startPos, endOffset).toString().trim();
  56. }
  57. function ETG_doExclude(e){
  58. if (e.target.id == "ETGexPhrase"){
  59. var ss = '+-"' + ETGtext.replace(/\s+/g, '+') + '"';
  60. ETG_reQry(ss, true);
  61. } else {
  62. var ss = '+-' + ETGtext.replace(/\s+/g, '+-');
  63. ETG_reQry(ss, true);
  64. }
  65. }
  66. // query modification borrowed from "Google site: Tool (Site r###lts / Exclude sites)"
  67. function ETG_reQry(d, go){
  68. // compute new URL
  69. if (!d) return;
  70. var cancel = false;
  71. var qa = window.location.href.substr(window.location.href.indexOf("?")+1).split("&");
  72. var updated = false;
  73. for (var j=qa.length-1; j>=0; j--){
  74. if (updated == false){
  75. if (qa[j].split("=")[0] == "q"){
  76. if (qa[j].indexOf(d) > -1 || qa[j].indexOf(d.replace(":", "%3A")) > -1) cancel = true;
  77. else var ipq = qa[j];
  78. qa[j] += d;
  79. updated = true;
  80. var substqry = qa[j];
  81. } else {
  82. if (qa[j].indexOf("#q=") > -1){
  83. if (qa[j].indexOf(d) > -1 || qa[j].indexOf(d.replace(":", "%3A")) > -1) cancel = true;
  84. else var ipq = qa[j].substr(qa[j].indexOf("#q=")+1);
  85. qa[j] += d;
  86. updated = true;
  87. var substqry = qa[j].substr(qa[j].indexOf("#q=")+1);
  88. }
  89. }
  90. } else {
  91. if (qa[j].split("=")[0] == "q"){
  92. if (go) qa[j] = ipq;
  93. else qa[j] = substqry;
  94. } else {
  95. if (qa[j].indexOf("#q=") > -1){
  96. if (go) qa[j] = qa[j].substr(0, qa[j].indexOf("#q=")+1) + ipq;
  97. else qa[j] = qa[j].substr(0, qa[j].indexOf("#q=")+1) + substqry;
  98. }
  99. }
  100. }
  101. }
  102. if (cancel != true) var locnew = window.location.href.substr(0, window.location.href.indexOf("?")+1) + qa.join("&");
  103. else locnew = "cancel";
  104. if (go) window.location.href = locnew;
  105. else return locnew;
  106. }