WordPress plugin support search

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

目前為 2016-09-09 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        WordPress plugin support search
// @namespace   wordpress.org
// @include     https://wordpress.org/support/plugin/*
// @version     1.1.0
// @grant       none
// @description Shows up on "Support" tab on WordPress plugin page and adds a form to search the support forum threads on Google; name of the plugin is added automatically.
// ==/UserScript==

var url = window.location.href; // e.g. https://wordpress.org/support/plugin/wp-super-cache
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);