Last active
January 3, 2023 10:06
-
-
Save AnthraX1/a158d4399d33c1d1757f6c2f1730cc3f to your computer and use it in GitHub Desktop.
Tampermonkey script to remove top search bar in Kibana 8.x and add column resize inputs in Discover.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Kibana resize | |
// @namespace http://tampermonkey.net/ | |
// @version 8.0 | |
// @description important: update matching url in the script. Script adds input fields for column width. Empty field resets width to auto. | |
// @author Anthr@X | |
// @include *://*/app/* | |
// @grant GM_addStyle | |
// @grant unsafeWindow | |
// ==/UserScript== | |
console.log('Kibana resize'); | |
var tableUpdated=false; | |
function countUnicode(str) { | |
// Use a regular expression to find all Unicode escape sequences in the string | |
const regex = /[\\]{1,}u(\w{4})/g; | |
let matches = str.match(regex); | |
// If no matches were found, return 0 | |
if (!matches) { | |
return 0; | |
} | |
// Otherwise, return the length of the array of matches | |
return matches.length; | |
} | |
function unescapeUnicodeRegex(str) { | |
return str.replace(/[\\]{1,}u(\w{4})/g, (match, grp) => | |
String.fromCharCode(parseInt(grp, 16)) | |
); | |
} | |
function unescapeUnicode(str) { | |
var count = countUnicode(str); | |
console.log("count: ", count); | |
if (count == 0) { | |
return str; | |
} | |
if (count > 100000) { | |
console.log("too many unicode chars, skipping: ", count); | |
return str; | |
} | |
if (count > 1000) { | |
return unescapeUnicodeLoop(str); | |
} | |
return unescapeUnicodeRegex(str); | |
} | |
function unescapeUnicodeLoop(str) { | |
let result = ''; | |
let i = 0; | |
while (i < str.length) { | |
if (str[i] === '\\' && str[i + 1] === 'u') { | |
// Found a Unicode escape sequence. | |
let codePoint = str.substring(i + 2, i + 6); | |
let value = parseInt(codePoint, 16); | |
if (value >= 0 && value <= 0x10FFFF) { | |
// Valid Unicode code point, append the corresponding character. | |
result += String.fromCharCode(value); | |
} else { | |
// Invalid code point, append the escape sequence as-is. | |
result += '\\u' + codePoint; | |
} | |
i += 6; | |
} else { | |
// Not a Unicode escape sequence, just append the character. | |
result += str[i]; | |
i++; | |
} | |
} | |
return result; | |
} | |
function hide_button_click($) { | |
console.log("hide_button_click"); | |
updateTable($); | |
var filter_bar = $(".euiFlexGroup.euiFlexGroup--alignItemsCenter.euiFlexGroup--directionRow.euiFlexGroup--wrap"); | |
if (filter_bar.css("display") != "none") { | |
filter_bar.hide(); | |
$("#hide_filter_btn").text("Show filters"); | |
} else { | |
filter_bar.show(); | |
$("#hide_filter_btn").text("Hide filters"); | |
} | |
} | |
function updateTableDivs($, divs) { | |
$.each(divs, (index, divEl) => { | |
if (divEl.innerHTML.startsWith("[{")) { | |
var div = $(divEl) | |
var start = performance.now(); | |
console.log("replacing text"); | |
div.text(unescapeUnicode(div.text())); | |
var end = performance.now(); | |
console.log("replace complete, took: ", end - start); | |
} | |
}); | |
} | |
function updateTableDivsAsync($, divs) { | |
// Define an array of deferred objects | |
var deferredObjects = []; | |
// Use $.each() to iterate over the array of divs | |
$.each(divs, function (index, divEl) { | |
// Check if the inner HTML of the div starts with "[{" | |
if (divEl.innerHTML.startsWith("[{")) { | |
// Create a deferred object for this div | |
var deferredObject = $.Deferred(); | |
// Do something with the div asynchronously, such as replacing its text | |
setTimeout(function () { | |
// Replace the text of the div | |
var div = $(divEl); | |
div.text(unescapeUnicode(div.text())); | |
// Resolve the deferred object | |
deferredObject.resolve(); | |
}, 0); | |
// Add the deferred object to the array | |
deferredObjects.push(deferredObject); | |
} | |
}); | |
var start = performance.now(); | |
// Use $.when() to execute a function when all of the deferred objects are resolved | |
$.when.apply($, deferredObjects).done(function () { | |
var end = performance.now(); | |
console.log("replace complete, took: ", end - start); | |
}); | |
} | |
function updateTable($) { | |
//add textbox for each column | |
$('.kbn-table th').each((i, thEl) => { | |
var th = $(thEl); | |
if (i == 0) { | |
return true; | |
}; | |
var input = $('<input class="size_input" type="number" size="3" min="20" max="10240" value="150"/><br/>').on('keyup', function () { | |
if (this.value.length >= 2) { | |
th.width(this.value); | |
} else if (!this.value) { | |
th.width(this.value); | |
} | |
}); | |
th.prepend(input); | |
}); | |
var divs = $("table.kbn-table").find("div.dscTruncateByHeight") | |
if (divs.length == 0) { | |
divs = $("div.euiDataGrid__virtualized").find("span.dscDiscoverGrid__cellValue") | |
} | |
if (divs.length != 0) { | |
updateTableDivsAsync($, divs) | |
tableUpdated=true; | |
} | |
//reset event handlers | |
$("table.kbn-table").off(); | |
$("table.kbn-table").on("DOMNodeInserted", function (event) { | |
if ($(event.target).is("tr.kbnDocTable__row")) { | |
divs = $(event.target).find("div.dscTruncateByHeight"); | |
updateTableDivsAsync($, divs); | |
} | |
}); | |
$("div.euiDataGrid__virtualized").off(); | |
$("div.euiDataGrid__virtualized").on("DOMNodeInserted", function (event) { | |
if ($(event.target).is("div.euiDataGridRowCell.euiDataGridRowCell--string")) { | |
divs = $(event.target).find("span.dscDiscoverGrid__cellValue"); | |
updateTableDivsAsync($, divs); | |
} | |
}); | |
} | |
setInterval(function () { | |
if (unsafeWindow.$('.kbn-table th input').length == 0 && !tableUpdated) { | |
updateTable(unsafeWindow.$); | |
} | |
if (unsafeWindow.$('#hide_filter_btn').length == 0) { | |
unsafeWindow.hide_button_click = hide_button_click; | |
unsafeWindow.$(".kbnRedirectCrossAppLinks").append('<button class="btn btn-primary" id="hide_filter_btn">Hide Filters</button>'); | |
const boundOnClick = hide_button_click.bind(null, unsafeWindow.$); | |
unsafeWindow.$("#hide_filter_btn").click(boundOnClick) | |
console.log("hide button added."); | |
} | |
}, 2000); | |
GM_addStyle('.euiHeader--fixed+.euiHeader--fixed{top: 0px !important;}'); | |
GM_addStyle('.kbnBody {padding-top: 49px !important;}'); | |
GM_addStyle('.euiHeader--dark {display:none !important;}'); | |
GM_addStyle('.dscPage { height: calc(100vh - 55px) !important;}'); | |
GM_addStyle('.dscPageContent__wrapper {padding: 0 0 0 0;}'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment