pjjackowski / WordPress plugin support search

// ==UserScript==
// @name        WordPress plugin support search
// @namespace   wordpress.org
// @include     https://wordpress.org/support/plugin/*
// @version     1.1.1
// @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.
// @grant       none
// ==/UserScript==

//  History:
//  1.0.0   initial release
//  1.1.0   fix for HTML changes on WordPress.org site
//  1.1.1   fix when URL ends up with a slash

// URL e.g. https://wordpress.org/support/plugin/wp-super-cache
var url = window.location.href.replace(/\/$/, ''); // trim trailing slash!
var pluginName = url.substr(url.lastIndexOf('/') + 1).split('-').join(' ');
var pluginNameWordCounter = pluginName.split(' ').length;

// clean up search input on window focus (i.e. remove `site:wordpress.org` and plugin name)
window.onfocus = function(e) {
    var query = document.getElementById('q').value;
    query = query.split(' ');
    query.splice(0, pluginNameWordCounter + 1);
    query = query.join(' ');
    document.getElementById('q').value = query;
}

var topics = document.getElementById('bbp-forum-0');
var searchForm = document.createElement('form');
var searchHintText = document.createTextNode('search support threads:');
    searchForm.setAttribute('method', 'get');
    searchForm.setAttribute('action', 'https://www.google.com/search');

var fieldset = document.createElement('fieldset');
    fieldset.setAttribute('style', 'width: 100%; height: 20px; margin-bottom: 35px;');

    fieldset.appendChild(searchHintText);

var searchText = document.createElement('input');
    searchText.setAttribute('type', 'text');
    searchText.setAttribute('id', 'q');
    searchText.setAttribute('name', 'q');
    searchText.setAttribute('style', 'font: 16px Verdana, Arial, Helvetica, sans-serif; padding: 5px 10px; width: 340px; margin: 0 20px;');

var target = document.createElement('input');
    target.setAttribute('type', 'checkbox');
    target.setAttribute('checked', 'checked');
    target.setAttribute('id', 'new_tab');
    target.setAttribute('style', 'margin-left: 15px;');

var targetLabel = document.createElement('label');
    targetLabel.setAttribute('for', 'new_tab');
    targetLabel.setAttribute('style', 'margin-left: 5px; cursor: pointer; vertical-align: 2px;');
    targetLabel.innerHTML = 'new tab';

var submitButton = document.createElement('input');
    submitButton.setAttribute('type', 'submit');
    submitButton.setAttribute('value', 'search');
    submitButton.setAttribute('class', 'button');
var submitButtonText = document.createTextNode('search');

    submitButton.appendChild(submitButtonText);

topics.parentNode.insertBefore(searchForm, topics);
    searchForm.appendChild(fieldset);
        fieldset.appendChild(searchText);
        fieldset.appendChild(submitButton);
        fieldset.appendChild(target);
        fieldset.appendChild(targetLabel);

searchText.focus();

// register listener on `submit`
searchForm.addEventListener('submit', function(event) {
    var searchTerm = document.getElementById('q').value;
    if (searchTerm == '') {
        alert('You need to enter a query!');
        searchText.focus();
        event.preventDefault();
    }
    else {
        // add `target="_blank"` if necessary
        if (document.getElementById('new_tab').checked) {
            searchForm.setAttribute('target', '_blank');
        }
        document.getElementById('q').value = 'site:wordpress.org ' + pluginName + ' ' + searchTerm;
    }
}, false);