Super-phrase - automatic "phrase search" on DuckDuckGo.

Adds a verbatim tick-box to the DuckDuckGo search bar.

当前为 2020-09-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Super-phrase - automatic "phrase search" on DuckDuckGo.
  3. // @namespace https://github.com/josefandersson/userscripts/
  4. // @version 1.2
  5. // @description Adds a verbatim tick-box to the DuckDuckGo search bar.
  6. // When ticked, anything typed in the box is auto-wrapped
  7. // in quote marks and thus "searched as a phrase".
  8. // Works on main home-page only. Use to form your initial phrase,
  9. // then you add additional search keywords and knock-outs in the
  10. // search-box on the results page.
  11. // @author Josef Andersson, under paid contract to myself.
  12. // @author Working regex for "phrase wrapping" then supplied to me by
  13. // Fechinsir on Fivver, again under paid contract to myself.
  14. // @match https://duckduckgo.com/*
  15.  
  16. // @run-at document-end
  17. // ==/UserScript==
  18.  
  19. if (location.pathname === '/') {
  20. const test = document.getElementById('content_homepage');
  21. if (test == null) {
  22. return;
  23. }
  24.  
  25. let form = document.getElementById('search_form_homepage');
  26. let input = form.querySelector('input');
  27.  
  28. form.addEventListener('submit', ev => {
  29. if (localStorage.getItem('forceVerbatim') === 'true') {
  30. input.value = input.value.replace(/(^.*$)/g, '"$1"');
  31. }
  32. }, {
  33. capture: true
  34. });
  35.  
  36. const div = document.createElement('div');
  37. const label = document.createElement('label');
  38. const checkbox = document.createElement('input');
  39. label.innerText = 'Verbatim';
  40. label.htmlFor = 'forceVerbatim'
  41. checkbox.id = 'forceVerbatim';
  42. checkbox.type = 'checkbox';
  43. checkbox.checked = localStorage.getItem('forceVerbatim') === 'true';
  44. div.appendChild(label);
  45. div.appendChild(checkbox);
  46. Object.assign(div.style, { margin:'-30px -3px 0 0', color:'#888', fontSize:'.8em', textAlign:'right' });
  47. Object.assign(checkbox.style, {})
  48.  
  49. form.parentElement.insertBefore(div, form);
  50.  
  51. checkbox.addEventListener('change', _ => {
  52. localStorage.setItem('forceVerbatim', checkbox.checked);
  53. });
  54. }