Skip to content

Instantly share code, notes, and snippets.

@silvers
Last active October 28, 2020 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save silvers/d7a4fd4c9beef37e1921345631808d72 to your computer and use it in GitHub Desktop.
Save silvers/d7a4fd4c9beef37e1921345631808d72 to your computer and use it in GitHub Desktop.
Confluence: Change from OR search to AND search.
## 2020/10/28 v0.4
* fix(suggest): submit -> click
## 2020/10/09 v0.3
* feature(replace): add on/off switch
* feature(suggest): suggest by first load
* feature(suggest): speedy suggest
## 2020/10/09 v0.2
* fix(*): fixed incorrect replace
* OR -> AND "OR" AND
* (aaa) -> "(aaa)"
* "double word" -> "double" "word"
* fix(suggest): ignore descendant search
## 2020/09/15 v0.1
* add confluence-replace-search.user.js
* add confluence-suggest-search.user.js
// ==UserScript==
// @name confluence-replace-search
// @namespace https://gist.github.com/silvers/
// @version 0.3
// @description Change from OR search to AND search in confluence search-panel.
// @author silvers
// @match https://wiki.*.org/*
// @grant none
// ==/UserScript==
// How to use
// 1. Install Tampermonkey( https://www.tampermonkey.net/ )
// 2. Click on "Raw" and install the script
// 3. Add matches; e.g. https://wiki.silvers.dev/* (Setting > Includes/Excludes > User matches)
(() => {
'use strict';
// https://github.com/facebook/react/issues/10135#issuecomment-314441175
let setReactValue = (element, value) => {
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set;
const prototype = Object.getPrototypeOf(element);
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
if (valueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
valueSetter.call(element, value);
}
element.dispatchEvent(new Event('input', { bubbles: true }));
};
let replaceQuery = (value) => {
return value
.replace(/^(.+)$/, " $1 ").replace(/(?<=(?:\s|\(|\)))([^ "():]+)(?=(?:\s|\(|\)))/g, "\"$1\"").replace(/"(AND|OR)"/g, "$1").replace(/(^ | $)/, '')
.replace(/\s+/g, ' ').replace(/([)"]) ([("])/g, "$1 AND $2");
};
let keyUpHandle = (e) => {
// Rerutn string...
if (!(window.localStorage.getItem('autoReplace') === 'true')) { return; }
// not fired IME composition
if (e.isComposing || e.keyCode === 229) { return; }
if (e.code === "Enter" || e.code === "Space") {
setReactValue(e.target, replaceQuery(e.target.value));
}
};
let input = document.querySelector("#quick-search-query");
if (input) {
input.addEventListener("focus", () => {
window.setTimeout(() => {
document.querySelector("#search-filter-input").addEventListener('keyup', keyUpHandle);
document.querySelector("#search-filter-input").parentNode.parentNode.insertAdjacentHTML('afterend', '<input id="switch-auto-replace" style="margin-left: 20px;" type="checkbox" checked="">Auto Replace</input>');
document.querySelector("#switch-auto-replace").checked = window.localStorage.getItem('autoReplace') === 'true' ? true : false;
document.querySelector("#switch-auto-replace").addEventListener('change', (e) => {
console.log(e.target.checked);
window.localStorage.setItem('autoReplace', e.target.checked);
});
}, 500);
});
}
})();
// ==UserScript==
// @name confluence-suggest-search
// @namespace https://gist.github.com/silvers/
// @version 0.4
// @description Suggest a search text with a whitespace separator as AND in confluence search page(/dosearchsite.action)
// @author silvers
// @match https://*/dosearchsite.action*
// @grant none
// ==/UserScript==
// How to use
// 1. Install Tampermonkey( https://www.tampermonkey.net/ )
// 2. Click on "Raw" and install the script
(() => {
'use strict';
let suggestSearchText = () => {
let input = document.querySelector("#query-string").value;
let suggestion = input
.replace(/^(.+)$/, " $1 ").replace(/(?<=(?:\s|\(|\)))([^ "():]+)(?=(?:\s|\(|\)))/g, "\"$1\"").replace(/"(AND|OR)"/g, "$1").replace(/(^ | $)/, '')
.replace(/\s+/g, ' ').replace(/([)"]) ([("])/g, "$1 AND $2");
let wrapper = document.querySelector("#suggestion-wrapper");
if (wrapper) { wrapper.parentNode.removeChild(wrapper); }
// No suggestions needed
if (input.trim() === suggestion.trim()) { return; }
document.querySelector("#search-results-header").insertAdjacentHTML('afterend', '<div id="suggestion-wrapper" style="margin: -20px 0 0 238px;">Did you mean: <a id="suggestion">' + suggestion + '</a></div>');
document.querySelector("#suggestion").addEventListener('click', (e) => {
document.querySelector("#query-string").value = e.target.innerText;
document.querySelector("#search-query-submit-button").click();
});
};
document.querySelector("#search-form").addEventListener("submit", suggestSearchText);
suggestSearchText();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment