// ==UserScript==
// @name Google Knowledge Graph ID
// @namespace https://www.brandonfowler.me
// @version 1.0.1
// @description View the ID of knowledge panels on Google search
// @author Brandon Fowler
// @match https://www.google.com/search*
// @grant none
// @run-at document-start
// @license MIT
// @downloadURL none
// ==/UserScript==
window.addEventListener('load', async () => {
let kpActionEl = document.querySelector('[class*="kp-"] > :first-child');
if (!kpActionEl) return;
let controller = await kpActionEl.__jscontroller,
id = controller.data.toJSON()[1];
if (!id) return;
let el = document.createElement('div'),
text = document.createElement('span'),
copy = document.createElement('span'),
link = document.createElement('a');
text.innerText = 'Knowledge Graph ID: ' + id;
text.style.marginRight = '8px';
text.style.verticalAlign = 'middle';
copy.title = 'Copy ID';
copy.style.cursor = 'pointer';
text.style.verticalAlign = 'middle';
copy.innerHTML = '';
copy.addEventListener('click', async () => {
await navigator.clipboard.writeText(id);
copy.innerHTML = '';
});
link.title = 'Link';
link.href = 'https://www.google.com/search?kgmid=' + id;
link.style.color = 'inherit';
text.style.verticalAlign = 'middle';
link.innerText = 'link';
link.innerHTML = '';
el.style.marginBottom = '6px';
el.append(text, copy, link);
kpActionEl.parentNode.before(el);
});