Last active
May 21, 2024 22:00
-
-
Save Kurotaku-sama/ce96f72ac5bb51c7246b51b3b18a30c7 to your computer and use it in GitHub Desktop.
Search results on the current page will be checked, if the domain name matches with a list of URLs. If it matches than they will be highlighted and ordered at the start of the result box
This file contains 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 Google prioritize and highlight results | |
// @name:de Google Priorisierung und Hervorhebung von Ergebnissen | |
// @namespace kurotaku.de | |
// @description Search results on the current page will be checked if the domain name matches with a list of urls, if it matches than they will be highlighted and ordered at the start of the result box | |
// @description:de Die Suchergebnisse auf der aktuellen Seite werden daraufhin überprüft, ob der Domainname mit einer Liste von URLs übereinstimmt; wenn dies der Fall ist, werden sie hervorgehoben und am Anfang der Ergebnisbox angeordnet | |
// @version 2.4.9 | |
// @author Kurotaku | |
// @license MIT | |
// @include https://www.google.*/search?* | |
// @icon https://www.google.com/favicon.ico | |
// @updateURL https://gist.github.com/Kurotaku-sama/ce96f72ac5bb51c7246b51b3b18a30c7/raw/Google%2520prioritize%2520and%2520highlight%2520results.user.js | |
// @downloadURL https://gist.github.com/Kurotaku-sama/ce96f72ac5bb51c7246b51b3b18a30c7/raw/Google%2520prioritize%2520and%2520highlight%2520results.user.js | |
// @require https://openuserjs.org/src/libs/sizzle/GM_config.js | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM_registerMenuCommand | |
// ==/UserScript== | |
let is_kp_wp_tab_overview = false; // If its a split up page | |
(async function() { | |
await init_gm_config(); // Initialize the configuration | |
// insert_gm_config_button(); // Add the button to the filter container to open the configuration menu | |
if(GM_config.get("script_enabled")) | |
start_prioritize(); // Start the main part of the Script | |
})(); | |
function init_gm_config() { | |
GM_registerMenuCommand('Settings', () => { | |
GM_config.open(); | |
}); | |
let frame = document.createElement('div'); | |
document.body.appendChild(frame); | |
GM_config.init( | |
{ | |
'id': 'configuration', | |
'title': 'Google prioritize and highlight results config', | |
'fields': | |
{ | |
'script_enabled': | |
{ | |
'label': 'Enable/Disable the prioritize Script', | |
'type': 'checkbox', | |
'default': true | |
}, | |
'url_terms': | |
{ | |
'label': 'Domain Terms (each domain must be written in a new line)', | |
'type': 'textarea', | |
'default': '' | |
}, | |
'styles': | |
{ | |
'label': 'CSS Styles', | |
'type': 'text', | |
'default': 'padding: 20px; border: cyan 2px solid' | |
} | |
}, | |
'events': { | |
'init': () => {GM_config.set("url_terms", GM_config.get("url_terms").replace(/^\s*$(?:\r\n?|\n)/gm, ""))}, // To prevent blank lines | |
'save': () => {location.reload()}, | |
}, | |
'frame': frame, | |
'css': '#configuration {height:auto !important; width:auto !important; padding:20px !important;max-height: 600px !important;max-width:500px !important;} #configuration .config_header {font-size:17pt; font-weight:bold} .field_label {display: block; text-align:center;margin-bottom:10px;} #configuration .config_var {margin-top:10px;} .config_var :nth-child(2) {width: 300px; margin:auto; display:block ;text-align:center;} #configuration_buttons_holder {text-align: center;} #configuration_field_url_terms {height: 150px; width: 300px; resize: none;}' | |
}); | |
} | |
function start_prioritize() { | |
// If its a split up page | |
is_kp_wp_tab_overview = document.querySelector("#search #kp-wp-tab-overview") !== null; | |
// Get searchbox container | |
let searchresultbox = is_kp_wp_tab_overview ? document.querySelector("#search #kp-wp-tab-overview") : document.querySelector("#rso > div:last-of-type"); | |
if(searchresultbox) { | |
// Adjust styles for rearangement of results | |
searchresultbox.style.display = "grid"; | |
searchresultbox.style.gridTemplateColumns = "100%"; | |
// Get Results | |
let results = get_resultlist(); | |
if(results) | |
results.forEach((obj, i) => { | |
let result = obj.result; | |
let index = obj.index; | |
style_result(result, index, (index === 0)); // (obj.index === 0) checks if its the last (first one in array) result that should be styled | |
}); | |
} | |
} | |
function prepare_url_terms() { | |
let url_terms = GM_config.get("url_terms").split("\n"); // Get urls from config and split them | |
url_terms = url_terms.map(function (el) {return el.trim();}); // Trim away the spaces | |
url_terms = url_terms.filter((el) => el !== ""); // Remove all empty lines (shouldn't happen anymore, but safe is safe) | |
// New reversed url_terms for correct ordering [NOT necessary NECESSARY ANYMORE DUE TO FILTER METHOD] | |
return url_terms.reverse(); | |
} | |
function style_result(result, index, last) { | |
result.style.order = -1 - index; // Order according to the index | |
result.setAttribute("style", result.getAttribute("style") + GM_config.get("styles")); | |
result.style.maxWidth = "100%"; // To prevent breaking out of the screen size | |
result.querySelector(".g").style.margin = "unset"; // Remove the margin of the results inside | |
result.querySelector(".g").style.width = "unset"; // Remove the fixed width (needed on split up pages) | |
result.style.margin = !last ? "unset" : "0 0 30px 0"; // Remove the margin of the results | |
} | |
function get_resultlist() { | |
// Get URL Terms | |
let reversed_url_terms = prepare_url_terms(); | |
// Get the searchresults | |
let searchresults = is_kp_wp_tab_overview ? document.querySelectorAll("#search #kp-wp-tab-overview div.TzHB6b") : document.querySelectorAll("#search div.MjjYud"); | |
if(searchresults) { | |
let matching_results = []; // Array for return | |
for(let i = 0; i < searchresults.length; i++) { | |
let searchresult = searchresults[i]; | |
let a_tag = searchresult.querySelector("div.yuRUbf a"); // Get URL element | |
if(a_tag) { | |
let url = a_tag.href.toLowerCase().split('/'); // Split all parts | |
let domain = url[2]; // Get only the domainname | |
// Check if result contains a string from the array | |
let url_index; | |
if(reversed_url_terms.some(function(v, index) { | |
url_index = index; // Asign current array index | |
return domain.indexOf(v) >= 0; // Check if Domain fits into url | |
})) { | |
// Insert matching searchresult | |
matching_results.push({result: searchresult, index : url_index}); | |
} | |
} | |
} | |
return matching_results; | |
} | |
else return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment