Advertisement
localblitz

Machine Readable Entity ID

Apr 14th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Machine-Readable Entity ID Bookmarklet */
  2. /*
  3.     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/
  4. */
  5.  
  6. // First, try to grab the DOM  element that contains the MREID. NOTE: This _will_ break when/if Google changes the markup of their SERPs.
  7. var knowledgeNode = document.querySelector("#rhs .kno-ftr > .kno-fb > div[data-async-context]");
  8. // 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.
  9. if (knowledgeNode !== null) {
  10.     // A. cardID will be equal to a cleaned up MREID.
  11.     var cardId = unescape(document.querySelector("#rhs .kno-ftr > .kno-fb > div[data-async-context]").getAttribute("data-async-context").split("card_id:")[1].split(";")[0]);
  12.     // B. Print a success message to the console.
  13.     console.log("%cSuccess! An MREID was found!","color: green; font-size: large");
  14.     console.log("The machine-readable entity ID for\nthis search entitiy is " + cardId);
  15.     // C. Print a success message to the SERP itself. So meta!
  16.     var node = document.createElement("strong");
  17.     var text = document.createTextNode("MREID is " + cardId + " | ");
  18.     node.appendChild(text);
  19.     var link = document.createElement("a");
  20.     link.setAttribute("href","https://trends.google.com/trends/explore?q=" + cardId);
  21.     link.setAttribute("target","_blank");
  22.     link.setAttribute("rel","noopener");
  23.     var linkText = document.createTextNode("Check via Google Trends");
  24.     link.appendChild(linkText);
  25.  
  26.     // D. Worth noting, we finde the element where Google reports "About X results" and append our message to that.
  27.     //document.getElementById("resultStats").appendChild(node);
  28.     document.querySelector("#kxbcct,#resultStats").appendChild(node);
  29.     document.querySelector("#kxbcct,#resultStats").appendChild(link);
  30. }
  31. // Third (AKA, fallback "Second"), we do something if no MREID was found on the SERP.
  32. else if (knowledgeNode == null) {
  33.     // See B above.
  34.     console.log("%cSorry! No MREID found!","color: red; font-size: large");
  35.     console.log("No machine-readable entity ID was found on this page. This is likely not an entity Google recognizes.");
  36.     // See C above.
  37.     var node = document.createElement("strong");
  38.     var text = document.createTextNode("No MREID found");
  39.     node.appendChild(text);
  40.     // See D above.
  41.     document.getElementById("resultStats").appendChild(node);  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement