Last active
May 6, 2018 09:56
-
-
Save GHolk/3154e8486cf987cf6ab84cae45c7dcc7 to your computer and use it in GitHub Desktop.
execute javascript in ptt chrome
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 chrome bbs javascript executor | |
// @namespace http://gholk.github.io/ | |
// @description press J in term.ptt.cc can run JavaScript | |
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js | |
// @match https://term.ptt.cc/* | |
// @match https://www.clam.ml/* | |
// @match https://www.ptt.cc/bbs/* | |
// @version 12 | |
// @grant none | |
// ==/UserScript== | |
// const output = 'hello world' | |
// const output = 'bookmarklet' | |
const output = 'user.js' | |
class JavascriptEvalator { | |
prompt() { | |
const script = this.getScript() | |
this.execute(script) | |
} | |
getArticle() { | |
if (location.href.match('www.ptt.cc')) { | |
return this.getPttWebArticle() | |
} | |
else return this.getPttChromeArticle() | |
} | |
getPttWebArticle() { | |
return $('#main-container').text() | |
} | |
getPttChromeArticle() { | |
return $('#mainContainer') | |
.children().get() | |
.map(row => $(row).text()) | |
.join('\n') | |
} | |
getScript() { | |
const article = this.getArticle() | |
const html = article.match(/<html[\S\s]*?<\/html>/) | |
const script = article.match(/<script[\S\s]*?<\/script>/) | |
return html || script | |
} | |
execute(script) { | |
return this.iframeObjectUrlExecute(script) | |
} | |
iframeObjectUrlExecute(script) { | |
const scriptBlob = new Blob([script]) | |
const scriptUrl = URL.createObjectURL(scriptBlob) | |
$('#bbsjs-container iframe').attr('src', scriptUrl) | |
URL.revokeObjectURL(scriptBlob) | |
$('#bbsjs-container').addClass('show') | |
} | |
evalExecute(script) { | |
const cleanScript = script | |
.replace(/^<script[\S\s]*?>/, '') | |
.replace(/<\/script>$/, '') | |
return eval(cleanScript) | |
} | |
windowExecute(script) { | |
const scriptWindow = window.open('about:blank') | |
const scriptDocument = scriptWindow.document | |
scriptDocument.write(script) | |
scriptDocument.close() | |
return scriptWindow | |
} | |
iframeExecute(script) { | |
$('#bbsjs-container').addClass('show') | |
const iframe = $('#bbsjs-container iframe').get(0) | |
iframe.contentDocument.write(script) | |
iframe.contentDocument.close() | |
return iframe | |
} | |
} | |
function promptJe() { | |
const je = new JavascriptEvalator() | |
je.prompt() | |
} | |
function registJe(listen) { | |
$(window).keydown((down) => { | |
if (listen(down)) { | |
try { | |
promptJe() | |
} | |
catch (bbsjsError) { | |
console.error(bbsjsError) | |
} | |
} | |
}) | |
} | |
if ($('#bbsjs-container').length == 0) initBbsjsFrame() | |
switch (output) { | |
case 'hello world': | |
alert('hello world!') | |
break | |
case 'bookmarklet': | |
promptJe() | |
break | |
case 'user.js': | |
registJe((keydown) => keydown.key == 'J') | |
break | |
} | |
function initBbsjsFrame() { | |
const $div = $('<div>').attr('id', 'bbsjs-container') | |
const homePageUrl = 'https://gholk.github.io/bbsjs.html' | |
$('<button>').text('move').appendTo($div).click((click) => { | |
const $container = $(click.target).parent() | |
$container.toggleClass('show') | |
}) | |
$('<a>').text('help') | |
.attr('target', 'bbsjs-iframe') | |
.attr('href', homePageUrl) | |
.appendTo($div) | |
$('<iframe>') | |
.attr('name', 'bbsjs-iframe') | |
.attr('src', homePageUrl) | |
.appendTo($div) | |
const cssText = ` | |
#bbsjs-container { | |
transition: 0.5s; | |
position: fixed; | |
top: 1em; | |
left: 1em; | |
z-index: 2; | |
background: white; | |
} | |
#bbsjs-container * { | |
display: none; | |
} | |
#bbsjs-container button { | |
display: inline; | |
margin: 0.5em; | |
color: black; | |
} | |
#bbsjs-container.show iframe { | |
border: none; | |
clear: both; | |
display: block; | |
width: 100%; | |
height: 25em; | |
} | |
#bbsjs-container.show a { | |
display: inline; | |
} | |
#bbsjs-container.show { | |
transition: 0.5s; | |
width: 40%; | |
top: 2em; | |
left: 30%; | |
z-index: 2; | |
} | |
` | |
$('<style>').text(cssText).appendTo($div) | |
$div.appendTo('body') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment