Created
September 11, 2012 17:30
-
-
Save masamitsu-murase/3700054 to your computer and use it in GitHub Desktop.
mruby with NPAPI plugin
This file contains hidden or 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
<script id="ruby_script" type="text/ruby"> | |
#<![CDATA[ | |
num = 0 | |
1000.times do |i| | |
num += i | |
end | |
hash = { result: num, input: $input } # $input has JavaScript function. | |
hash[:input].call({ a: num }) # JS function is called. | |
$result = hash[:result] | |
# ]]> | |
</script> | |
<script type="text/javascript"> | |
// Global variable $input is shared in mruby and JavaScript. | |
$input = function(i){ | |
add("test: " + i.a); // i is JS object converted from mruby hash. | |
}; | |
// Add div element. | |
function add(str) | |
{ | |
var elem = document.createElement("div"); | |
document.getElementById("result").appendChild(elem); | |
var text_elem = document.createTextNode(str); | |
elem.appendChild(text_elem); | |
} | |
</script> | |
<form> | |
<!-- 'ruby(id)' method runs mruby script in script tag. --> | |
<input type=button value="test" onclick='alert(ruby("ruby_script"))' /> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment