-
Notifications
You must be signed in to change notification settings - Fork 0
/
anchors.bookmarklet
1 lines (1 loc) · 5.99 KB
/
anchors.bookmarklet
1
javascript:(function()%7Bvar%20AnchorExtensionJavaScriptClass%20%3D%20function%20()%20%7B%7D%3Bif%20(typeof(String.prototype.trim)%20%3D%3D%3D%20%22undefined%22)%20%7BString.prototype.trim%20%3D%20function%20()%20%7Breturn%20String(this).replace(%2F%5E%5Cs%2B%7C%5Cs%2B%24%2Fg%2C%20'')%7D%7DAnchorExtensionJavaScriptClass.prototype%20%3D%20%7Brun%3A%20function%20(arguments)%20%7Bvar%20results%20%3D%20%7B%22baseURI%22%3A%20document.baseURI%7D%3Bvar%20cleanBaseURI%20%3D%20document.baseURI%3Bvar%20lastSlash%20%3D%20cleanBaseURI.lastIndexOf('%2F')%3Bvar%20lastHash%20%3D%20cleanBaseURI.lastIndexOf('%23')%3Bif%20(lastHash%20%3E%20lastSlash)%20%7BcleanBaseURI%20%3D%20cleanBaseURI.substring(0%2C%20lastHash)%7Dresults.span%20%3D%20AnchorExtensionJavaScriptClass.parseWikipedia(document%2C%20cleanBaseURI)%3Bresults.header%20%3D%20AnchorExtensionJavaScriptClass.parseHeaders(document%2C%20cleanBaseURI)%3Bresults.a%20%3D%20AnchorExtensionJavaScriptClass.parseAnchors(document%2C%20cleanBaseURI)%3Barguments.completionFunction(results)%7D%2C%20finalize%3A%20function%20(arguments)%20%7Bvar%20el%20%3D%20document.getElementById(arguments%5B%22selectedId%22%5D)%3Bif%20(el%20!%3D%20null)%20%7Bel.scrollIntoView()%7D%20else%20%7Bvar%20els%20%3D%20document.getElementsByName(arguments%5B%22selectedId%22%5D)%3Bif%20(els%20!%3D%20null%20%26%26%20els.length%20%3E%200)%20%7Bels%5B0%5D.scrollIntoView()%7D%7D%7D%7D%3BAnchorExtensionJavaScriptClass.completion%20%3D%20function%20(results)%20%7Bvar%20formatted%20%3D%20%22%3Cbody%3E%22%3Bfor%20(var%20element%20in%20results)%20%7Bformatted%20%2B%3D%20%22%3Ch1%3E%22%20%2B%20element%20%2B%20%22%3C%2Fh1%3E%22%3Bif%20(element%20%3D%3D%3D%20%22baseURI%22)%20%7Bformatted%20%2B%3D%20%22%3Ca%20href%3D%5C%22%22%20%2B%20results%5Belement%5D%20%2B%20%22%5C%22%3E%22%20%2B%20results%5Belement%5D%20%2B%20%22%3C%2Fa%3E%22%7D%20else%20%7Bformatted%20%2B%3D%20%22%3Cul%3E%22%3Bfor%20(var%20subelement%20in%20results%5Belement%5D)%20%7Bvar%20label%20%3D%20%22...%22%3Bvar%20url%20%3D%20%22.%22%3Btry%20%7Bvar%20mofo%20%3D%20results%5Belement%5D%5Bsubelement%5D%3Blabel%20%3D%20decodeURI(mofo.substring(0%2C%20mofo.lastIndexOf('%5Ct')))%3Burl%20%3D%20mofo.substring(mofo.lastIndexOf('%5Ct')%2C%20mofo.length)%3B%7D%20catch%20(e)%20%7Bconsole.log(JSON.stringify(mofo))%3B%7Dformatted%20%2B%3D%20%22%3Cli%3E%22%3Bformatted%20%2B%3D%20label%3Bformatted%20%2B%3D%20%22%3Ca%20href%3D%5C%22%22%20%2B%20url%20%2B%20%22%5C%22%3E%22%20%2B%20url%20%2B%20%22%3C%2Fa%3E%22%3Bformatted%20%2B%3D%20%22%3C%2Fli%3E%22%7Dformatted%20%2B%3D%20%22%3C%2Ful%3E%22%7D%7Dformatted%20%2B%3D%20%22%3C%2Fbody%3E%22%3Bdocument.body.innerHTML%20%3D%20formatted%7D%3BAnchorExtensionJavaScriptClass.parseWikipedia%20%3D%20function%20(document%2C%20cleanBaseURI)%20%7Bvar%20anchors%20%3D%20%5B%5D%3Bif%20(cleanBaseURI.includes(%22wikipedia%22))%20%7Bvar%20links%20%3D%20document.getElementsByTagName(%22SPAN%22)%3Bfor%20(var%20i%20%3D%200%3B%20i%20%3C%20links.length%3B%20i%2B%2B)%20%7Bvar%20link%20%3D%20links%5Bi%5D%3Bif%20(!link.hasAttribute(%22id%22))continue%3Bif%20(link.childNodes.length%20%3D%3D%200)continue%3Bvar%20label%20%3D%20link.childNodes%5B0%5D.nodeValue%3Bif%20(label%20%3D%3D%20null%20%7C%7C%20label.length%20%3D%3D%200)%20%7Blabel%20%3D%20link.childNodes%5B0%5D.id%7Dlabel.trim()%3Banchors.push(encodeURI(label)%20%2B%20%22%5Ct%22%20%2B%20cleanBaseURI%20%2B%20%22%23%22%20%2B%20link.getAttribute(%22id%22))%7D%7Dreturn%20anchors%7D%3BAnchorExtensionJavaScriptClass.parseHeaders%20%3D%20function%20(document%2C%20cleanBaseURI)%20%7Bvar%20anchors%20%3D%20%5B%5D%3Banchors.push(encodeURI(document.title.trim())%20%2B%20%22%5Ct%22%20%2B%20cleanBaseURI%20%2B%20%22%23%22)%3Bvar%20links%20%3D%20document.querySelectorAll(%22h1%2C%20h2%2C%20h3%2C%20h4%2C%20h5%2C%20h6%22)%3Bfor%20(var%20i%20%3D%200%3B%20i%20%3C%20links.length%3B%20i%2B%2B)%20%7Bvar%20link%20%3D%20links%5Bi%5D%3Bif%20(!link.hasAttribute(%22id%22))continue%3Bvar%20label%20%3D%20link.textContent%20%3F%20link.textContent%20%3A%20%22%22%3Blabel%20%3D%20label.trim()%3Blabel%20%3D%20link.nodeName%20%2B%20%22%20%3A%20%22%20%2B%20label%3Blabel%20%3D%20label.trim()%3Blabel%20%3D%20encodeURI(label)%3Banchors.push(label%20%2B%20%22%5Ct%22%20%2B%20cleanBaseURI%20%2B%20%22%23%22%20%2B%20link.getAttribute(%22id%22))%7Dreturn%20anchors%7D%3BAnchorExtensionJavaScriptClass.parseAnchors%20%3D%20function%20(document%2C%20cleanBaseURI)%20%7Bvar%20anchors%20%3D%20%5B%5D%3Bvar%20duplicateSet%20%3D%20new%20Set()%3Bvar%20links%20%3D%20document.getElementsByTagName(%22A%22)%3Bfor%20(var%20i%20%3D%200%3B%20i%20%3C%20links.length%3B%20i%2B%2B)%20%7Bvar%20link%20%3D%20links%5Bi%5D%3Bvar%20linkhref%20%3D%20link.href%3Bif%20(linkhref.lastIndexOf('%23')%20%3C%200%20%26%26%20!link.hash)%20%7Bif%20(link.hasAttribute(%22name%22))%20%7Blinkhref%20%3D%20cleanBaseURI%20%2B%20%22%23%22%20%2B%20link.getAttribute(%22name%22)%7D%20else%20if%20(link.hasAttribute(%22id%22))%20%7Blinkhref%20%3D%20cleanBaseURI%20%2B%20%22%23%22%20%2B%20link.getAttribute(%22id%22)%7D%20else%20%7Bcontinue%7D%7Dvar%20label%20%3D%20link.nodeName%20%2B%20%22%20-%20%22%20%2B%20link.textContent%20%3F%20link.textContent%20%3A%20%22%22%3Bif%20(label%20%3D%3D%20null%20%7C%7C%20label.length%20%3D%3D%200)%20%7Bif%20(linkhref.lastIndexOf('%23')%20%3E%3D%200)%20%7Blabel%20%3D%20linkhref.substring(linkhref.lastIndexOf('%23')%2C%20linkhref.length)%7D%7Dlabel%20%3D%20label.trim()%3Bif%20(cleanBaseURI.includes(%22wikipedia.org%22)%20%26%26%20label.length%20%3D%3D%201)%20%7Bcontinue%7Dlabel%20%3D%20encodeURI(label)%3Bvar%20pushstring%20%3D%20label%20%2B%20%22%5Ct%22%20%2B%20linkhref%3Bif%20(!duplicateSet.has(pushstring))%20%7Banchors.push(pushstring)%3BduplicateSet.add(pushstring)%7D%7Dreturn%20anchors%7D%3BAnchorExtensionJavaScriptClass.fun%20%3D%20function%20(arguments)%20%7Bvar%20callback%20%3D%20AnchorExtensionJavaScriptClass.completion%3Bvar%20go%20%3D%20%7B%22completionFunction%22%3A%20callback%7D%3BAnchorExtensionJavaScriptClass.prototype.run.call(null%2C%20go)%7D%3Bvar%20ExtensionPreprocessingJS%20%3D%20new%20AnchorExtensionJavaScriptClass%3BAnchorExtensionJavaScriptClass.fun()%7D)()