Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Machine-Readable Entity ID Bookmarklet */
- /*
- Welcome! Thanks for checking this out. This JavaScript bookmarklet will help you easily find the MREID (when available) on a Google SERP. Questions can be left as comments at: https://www.upbuild.io/blog/machine-readable-entity-ids-seo/
- */
- // First, try to grab the DOM element that contains the MREID. NOTE: This _will_ break when/if Google changes the markup of their SERPs.
- var knowledgeNode = document.querySelector("#rhs .kno-ftr > .kno-fb > div[data-async-context]");
- // Second, check that something was actually found above. If we try this on a null variable, we'll have issues so we need to avoid doing that.
- if (knowledgeNode !== null) {
- // A. cardID will be equal to a cleaned up MREID.
- var cardId = unescape(document.querySelector("#rhs .kno-ftr > .kno-fb > div[data-async-context]").getAttribute("data-async-context").split("card_id:")[1].split(";")[0]);
- // B. Print a success message to the console.
- console.log("%cSuccess! An MREID was found!","color: green; font-size: large");
- console.log("The machine-readable entity ID for\nthis search entitiy is " + cardId);
- // C. Print a success message to the SERP itself. So meta!
- var node = document.createElement("strong");
- var text = document.createTextNode("MREID is " + cardId + " | ");
- node.appendChild(text);
- var link = document.createElement("a");
- link.setAttribute("href","https://trends.google.com/trends/explore?q=" + cardId);
- link.setAttribute("target","_blank");
- link.setAttribute("rel","noopener");
- var linkText = document.createTextNode("Check via Google Trends");
- link.appendChild(linkText);
- // D. Worth noting, we finde the element where Google reports "About X results" and append our message to that.
- //document.getElementById("resultStats").appendChild(node);
- document.querySelector("#kxbcct,#resultStats").appendChild(node);
- document.querySelector("#kxbcct,#resultStats").appendChild(link);
- }
- // Third (AKA, fallback "Second"), we do something if no MREID was found on the SERP.
- else if (knowledgeNode == null) {
- // See B above.
- console.log("%cSorry! No MREID found!","color: red; font-size: large");
- console.log("No machine-readable entity ID was found on this page. This is likely not an entity Google recognizes.");
- // See C above.
- var node = document.createElement("strong");
- var text = document.createTextNode("No MREID found");
- node.appendChild(text);
- // See D above.
- document.getElementById("resultStats").appendChild(node);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement