Skip to content

Instantly share code, notes, and snippets.

@btaillon-coveo
Last active March 10, 2022 15:47
Show Gist options
  • Save btaillon-coveo/af627d1206dd5c687825f9a9b3cc04a5 to your computer and use it in GitHub Desktop.
Save btaillon-coveo/af627d1206dd5c687825f9a9b3cc04a5 to your computer and use it in GitHub Desktop.
Tempermonkey Script - Get result source
// ==UserScript==
// @name Get result source
// @description gets the source of a result.
// @namespace https://github.com/btaillon
// @updateURL https://gist.githubusercontent.com/btaillon/af627d1206dd5c687825f9a9b3cc04a5/raw/get-result-source.user.js
// @downloadURL https://gist.githubusercontent.com/btaillon/af627d1206dd5c687825f9a9b3cc04a5/raw/get-result-source.user.js
// @supportURL https://gist.githubusercontent.com/btaillon/af627d1206dd5c687825f9a9b3cc04a5
// @version 0.2
// @author btaillon
// @match *://*/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
/**
* @param {Element | ShadowRoot} element
* @returns {Element | ShadowRoot | null}
*/
function getParent(element) {
if (element.parentNode) {
return element.parentNode;
}
if (element instanceof ShadowRoot) {
return element.host;
}
return null;
}
/**
* @param {Element | ShadowRoot} element
* @returns {(Element & { result: any }) | null}
*/
function getAncestorResult(element) {
if (!element || element === document.body) {
return null;
}
if (element.tagName === "ATOMIC-RESULT") {
return element;
}
return getAncestorResult(getParent(element));
}
const searchInterface = document.querySelector("atomic-search-interface");
if (!searchInterface) {
return;
}
searchInterface.addEventListener(
"click",
(e) => {
if (!e.altKey) {
return;
}
const parentResult = getAncestorResult(e.originalTarget);
if (!parentResult) {
return;
}
console.info(parentResult.result);
},
{ capture: true }
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment