Last active
November 10, 2018 21:55
-
-
Save noromanba/2897358 to your computer and use it in GitHub Desktop.
auto syntax highlighting by google-code-prettify with Infinite Scrolling for UserScript/Bookmarklet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name addicted to google-code-prettify | |
// @namespace https://www.hatena.ne.jp/noromanba/ | |
// @description auto syntax highlighting by google-code-prettify with Infinite Scrolling for UserScript/Bookmarklet | |
// @include http://let.hatelabo.jp/* | |
// @include http://let.st-hatelabo.com/* | |
// @include https://raw.githubusercontent.com/* | |
// @include https://gist.githubusercontent.com/ | |
// @include https://bitbucket.org/*/raw/* | |
// @exclude http://let.hatelabo.jp/help* | |
// @exclude https://github.com/* | |
// @exclude *://code.google.com/p/* | |
// @exclude http://sourceforge.jp/projects/* | |
// @exclude https://bitbucket.org/* | |
// @exclude *://*.tld/*jquery*.js | |
// @exclude https://metacpan.org/* | |
// @exclude *://bazaar.launchpad.net/* | |
// @grant none | |
// @noframes | |
// @run-at document-end | |
// @version 2017.12.16.0 | |
// @homepage https://gist.github.com/noromanba/2897358 | |
// @downloadURL https://gist.github.com/noromanba/2897358/raw/addicted-to-google-code-prettify.user.js | |
// @license MIT License https://nrm.mit-license.org/2012 | |
// @author noromanba https://noromanba.github.com | |
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Five-interlaced-pentagons.svg/256px-Five-interlaced-pentagons.svg.png | |
// @include *://*.tld/*.bsh | |
// @include *://*.tld/*.c | |
// @include *://*.tld/*.cc | |
// @include *://*.tld/*.cpp | |
// @include *://*.tld/*.cs | |
// @include *://*.tld/*.csh | |
// @include *://*.tld/*.cyc | |
// @include *://*.tld/*.cv | |
// @include *://*.tld/*.java | |
// @include *://*.tld/*.js | |
// @include *://*.tld/*.m | |
// @include *://*.tld/*.mxml | |
// @include *://*.tld/*.perl | |
// @include *://*.tld/*.pl | |
// @include *://*.tld/*.pm | |
// @include *://*.tld/*.py | |
// @include *://*.tld/*.rb | |
// @include *://*.tld/*.sh | |
// @include *://*.tld/*.xhtml | |
// @include *://*.tld/*.xml | |
// @include *://*.tld/*.xsl | |
// ==/UserScript== | |
// TODO replace insane @include to RegExp | |
// http://wiki.greasespot.net/Include_and_exclude_rules#Regular_Expressions | |
// http://wiki.greasespot.net/Magic_TLD | |
// BUG Greasemonkey Magic TLD regression | |
// Magic TLD is did not work w/ wildcarded-subdomain; e.g. | |
// NG @include http://*.blogspot.tld | |
// OK @include https://googleblog.blogspot.tld | |
// https://gist.github.com/noromanba/f079d517b3d38d91010383d919ffff30 | |
// all `*://*.tld/*.lang` did not work | |
// TBD coexisting whitelist with a blacklist | |
// - https://bitbucket.org include/exclude | |
// - <at>include "Other languages are supported via extensions" sections | |
// - https://github.com/google/code-prettify#for-which-languages-does-it-work | |
// - https://github.com/google/code-prettify/blob/master/README.md#for-which-languages-does-it-work | |
// TBD detect CSP blocking; Content Script and Bookmarklet | |
// - http://ptech.g.hatena.ne.jp/noromanba/20160429/1461908465#csp-vs-js | |
// - https://blog.mono0x.net/blog/2012/03/15/content-security-policy/ | |
// - https://blog.mono0x.net/blog/2013/07/13/content-security-policy/ | |
// Icon (Public domain by AnonMoos) | |
// https://commons.wikimedia.org/wiki/File%3AFive-interlaced-pentagons.svg | |
// Devel | |
// https://gist.github.com/noromanba/2897358 | |
(() => { | |
'use strict'; | |
'https://gist.github.com/noromanba/2897358'; | |
// @noframes alternative to Vanilla JS e.g. use in Bookmarklet | |
// run only in top-frame (w/o iframes) c.f. | |
// https://stackoverflow.com/questions/1677558/greasemonkey-against-an-iframe-using-include-does-this-work/1679110#1679110 | |
// problem page e.g. | |
// https://www.youtube.com/results?search_query=WORD&sm=3 | |
if (window.top !== window.self) return; | |
const addCSS = (() => { | |
const wall = document.head || document.body || document.documentElement; | |
// TBD set request timeout threshold | |
return (url) => { | |
wall.appendChild(Object.assign(document.createElement('link'), { | |
rel: 'stylesheet', | |
type: 'text/css', | |
href: url, | |
})); | |
}; | |
})(); | |
const addStyle = (() => { | |
const wall = document.head || document.body || document.documentElement; | |
const style = wall.appendChild(Object.assign(document.createElement('style'), { | |
type: 'text/css', | |
charset: 'utf-8', | |
})); | |
return (css) => { | |
style.appendChild(document.createTextNode(css + '\n')); | |
}; | |
})(); | |
// https://github.com/google/code-prettify/blob/master/docs/getting_started.md | |
// https://cdn.rawgit.com/google/code-prettify/master/styles/index.html | |
const style = { | |
prettify: 'https://cdn.rawgit.com/google/code-prettify/master/src/prettify.css', | |
sunburst: 'https://cdn.rawgit.com/google/code-prettify/master/styles/sunburst.css', | |
desert: 'https://cdn.rawgit.com/google/code-prettify/master/styles/desert.css', | |
obsidian: 'https://cdn.rawgit.com/google/code-prettify/master/styles/sons-of-obsidian.css', | |
doxy: 'https://cdn.rawgit.com/google/code-prettify/master/styles/doxy.css', | |
}; | |
// TBD configurable | |
addCSS(style.sunburst); | |
// keep codearea metrics | |
addStyle(` | |
.prettyprint { | |
width: initial !important; | |
margin: initial !important; | |
} | |
`); | |
// TODO keep padding | |
addStyle(` | |
.prettyprint { | |
padding: initial !important; | |
} | |
`); | |
// TBD feedback indicator | |
// debouncing function call w/ queue c.f. | |
// http://javascript.g.hatena.ne.jp/edvakf/20100204/1265312155 | |
let timer, queue = []; | |
const prettify = (context) => { | |
queue.push(context); | |
clearTimeout(timer); | |
timer = setTimeout(() => { | |
queue.forEach(node => { | |
// TODO length check e.g. | |
// code.textContent.length | code.textContent.split('\n').join().length | |
node.querySelectorAll([ | |
'code:not(.prettyprint)', | |
'pre:not(.prettyprint):not(.code-diff)', | |
]).forEach(code => { | |
code.classList.add('prettyprint'); | |
// If you needs line number, add slash; "/*" -> "//*" | |
/* | |
code.classList.add('linenums'); | |
//*/ | |
// keep inline* | |
// TBD String#startsWith() | |
if (node.style.display.includes('inline') || | |
getComputedStyle(node).display.includes('inline')) { | |
addStyle(` | |
.prettyprint { | |
display: initial !important; | |
} | |
`); | |
} | |
}); | |
}); | |
// heaviness | |
// c.f. Asynchronous Module Definition (AMD) API | |
// https://github.com/amdjs/amdjs-api/wiki/AMD | |
window.PR.prettyPrint(); | |
queue = []; | |
}, 10); | |
}; // /prettify() | |
// @include alternative to Vanilla JS e.g. use in Bookmarklet c.f. | |
// http://let.hatelabo.jp/noromanba/let/hJmehqPZh6wf | |
const require = (urls) => { | |
return Array.from(urls, url => { | |
return document.body.appendChild(Object.assign(document.createElement('script'), { | |
src: url, | |
charset: 'utf-8', | |
})); | |
}); | |
}; | |
require([ | |
'https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js', | |
]).pop().addEventListener('load', () => { | |
prettify(document.body); | |
}); | |
new MutationObserver(records => { | |
records.forEach(record => { | |
const node = record.target; | |
if (!node.querySelectorAll) return; | |
prettify(node); | |
}); | |
}).observe(document.body, { childList: true, subtree: true }); | |
//*/ | |
})(); | |
// Bookmarklet | |
// http://userjs.up.seesaa.net/js/bookmarklet.html | |
/* | |
javascript:(()=>{'use strict';'https://gist.github.com/noromanba/2897358';if(window.top!==window.self)return;const addCSS=(()=>{const wall=document.head||document.body||document.documentElement;return(url)=>{wall.appendChild(Object.assign(document.createElement('link'),{rel:'stylesheet',type:'text/css',href:url,}));};})();const addStyle=(()=>{const wall=document.head||document.body||document.documentElement;const style=wall.appendChild(Object.assign(document.createElement('style'),{type:'text/css',charset:'utf-8',}));return(css)=>{style.appendChild(document.createTextNode(css+'\n'));};})();const style={prettify:'https://cdn.rawgit.com/google/code-prettify/master/src/prettify.css',sunburst:'https://cdn.rawgit.com/google/code-prettify/master/styles/sunburst.css',desert:'https://cdn.rawgit.com/google/code-prettify/master/styles/desert.css',obsidian:'https://cdn.rawgit.com/google/code-prettify/master/styles/sons-of-obsidian.css',doxy:'https://cdn.rawgit.com/google/code-prettify/master/styles/doxy.css',};addCSS(style.sunburst);addStyle(`.prettyprint{width:initial!important;margin:initial!important;}`);addStyle(`.prettyprint{padding:initial!important;}`);let timer,queue=[];const prettify=(context)=>{queue.push(context);clearTimeout(timer);timer=setTimeout(()=>{queue.forEach(node=>{node.querySelectorAll(['code:not(.prettyprint)','pre:not(.prettyprint):not(.code-diff)',]).forEach(code=>{code.classList.add('prettyprint');code.classList.add('linenums');if(node.style.display.includes('inline')||getComputedStyle(node).display.includes('inline')){addStyle(`.prettyprint{display:initial!important;}`);}});});window.PR.prettyPrint();queue=[];},10);};const require=(urls)=>{return Array.from(urls,url=>{return document.body.appendChild(Object.assign(document.createElement('script'),{src:url,charset:'utf-8',}));});};require(['https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js',]).pop().addEventListener('load',()=>{prettify(document.body);});new MutationObserver(records=>{records.forEach(record=>{const node=record.target;if(!node.querySelectorAll)return;prettify(node);});}).observe(document.body,{childList:true,subtree:true});})(); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment