WordPress plugin support search

Shows up on "Support" and "Reviews" tabs on WordPress plugin page and adds a form to search the support forum threads on Google; name of the plugin is added automatically.

  1. // ==UserScript==
  2. // @name WordPress plugin support search
  3. // @namespace wordpress.org
  4. // @include https://wordpress.org/support/plugin/*
  5. // @version 1.1.2
  6. // @description Shows up on "Support" and "Reviews" tabs on WordPress plugin page and adds a form to search the support forum threads on Google; name of the plugin is added automatically.
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. // History:
  11. // 1.0.0 initial release
  12. // 1.1.0 fix for HTML changes on WordPress.org site
  13. // 1.1.1 fix when URL ends up with a slash
  14. // 1.1.2 fix for HTML changes on WordPress.org site
  15. // fixed finding plugin's name (on subsequent forum pages)
  16. // plugin's name in query is surrounded with quotes
  17.  
  18. // URL e.g. https://wordpress.org/support/plugin/wp-super-cache/page/2
  19. var pluginName = window.location.href.split('/')[5].split('-').join(' ');
  20. var pluginNameWordCounter = pluginName.split(' ').length;
  21.  
  22. // clean up search input on window focus (i.e. remove `site:wordpress.org` and plugin name)
  23. window.onfocus = function(e) {
  24. var query = document.getElementById('q').value;
  25. query = query.split(' ');
  26. query.splice(0, pluginNameWordCounter + 1);
  27. query = query.join(' ');
  28. document.getElementById('q').value = query;
  29. }
  30.  
  31. var topics = document.getElementById('bbp-forum-0');
  32. var searchForm = document.createElement('form');
  33. var searchHintText = document.createTextNode('search support threads:');
  34. searchForm.setAttribute('method', 'get');
  35. searchForm.setAttribute('action', 'https://www.google.com/search');
  36.  
  37. var fieldset = document.createElement('fieldset');
  38. fieldset.setAttribute('style', 'width: 100%; margin-bottom: 35px; font-size: 14px; border: none;');
  39.  
  40. fieldset.appendChild(searchHintText);
  41.  
  42. var searchText = document.createElement('input');
  43. searchText.setAttribute('type', 'text');
  44. searchText.setAttribute('id', 'q');
  45. searchText.setAttribute('name', 'q');
  46. searchText.setAttribute('style', 'font: 16px Verdana, Arial, Helvetica, sans-serif; padding: 5px 10px; width: 250px; margin: 0 20px;');
  47.  
  48. var target = document.createElement('input');
  49. target.setAttribute('type', 'checkbox');
  50. target.setAttribute('checked', 'checked');
  51. target.setAttribute('id', 'new_tab');
  52. target.setAttribute('style', 'margin-left: 15px;');
  53.  
  54. var targetLabel = document.createElement('label');
  55. targetLabel.setAttribute('for', 'new_tab');
  56. targetLabel.setAttribute('style', 'margin-left: 5px; cursor: pointer; vertical-align: 2px; font-size: 14px;');
  57. targetLabel.innerHTML = 'new tab';
  58.  
  59. var submitButton = document.createElement('input');
  60. submitButton.setAttribute('type', 'submit');
  61. submitButton.setAttribute('value', 'search');
  62. submitButton.setAttribute('style', 'margin-top: 3px;');
  63. var submitButtonText = document.createTextNode('search');
  64.  
  65. submitButton.appendChild(submitButtonText);
  66.  
  67. topics.parentNode.insertBefore(searchForm, topics);
  68. searchForm.appendChild(fieldset);
  69. fieldset.appendChild(searchText);
  70. fieldset.appendChild(submitButton);
  71. fieldset.appendChild(target);
  72. fieldset.appendChild(targetLabel);
  73.  
  74. searchText.focus();
  75.  
  76. // register listener on `submit`
  77. searchForm.addEventListener('submit', function(event) {
  78. var searchTerm = document.getElementById('q').value;
  79. if (searchTerm == '') {
  80. alert('You need to enter a query!');
  81. searchText.focus();
  82. event.preventDefault();
  83. }
  84. else {
  85. // add `target="_blank"` if necessary
  86. if (document.getElementById('new_tab').checked) {
  87. searchForm.setAttribute('target', '_blank');
  88. }
  89. document.getElementById('q').value = 'site:wordpress.org "' + pluginName + '" ' + searchTerm;
  90. }
  91. }, false);