如何通過終端或其他方式將參數傳遞給 JavaScriptCore/Console? (How to pass an argument to JavaScriptCore/Console via terminal, or otherwise?)


問題描述

如何通過終端或其他方式將參數傳遞給 JavaScriptCore/Console? (How to pass an argument to JavaScriptCore/Console via terminal, or otherwise?)

JSC seems like the simpler-and-easier, more portable, and quasi-universally-installed - obvious alternative to the node.js's of the world... I have been able to figure out the basics, but there is almost nada floating around out there about it (why?), so here's a simple problem I'm hoping someone can clarify..

I have a nice littlejavascript "class" in a .js file that starts out like this...  BTW, it takes a Hex Color code and spits out a "named" color.  Neat.  Sample Usage:

<script type="text/javascript" src="ntc.js"></script>
<script type="text/javascript">
var n_match  = ntc.name("#6195ED");
    n_rgb        = n_match[0]; // This is the RGB value of the closest matching color
    n_name       = n_match[1]; // This is the text string for the name of the match
    n_exactmatch = n_match[2]; // True if exact color match, False if close-match
alert(n_match);
</script>

The code starts out with, and ends like so...

var ntc = {
     init: function() {
     var color, rgb, hsl;
  ⤹
}
ntc.init();

I am able, with little muss or fuss, to hardcode some values, at the BOTTOM of this document, like so...

var n_match = ntc.name("#000000");
print(n_match);

and run the code simply and easily from a terminal...

/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc -d ntc.js ↩

#000000,Black,true

However, for the life of me, I cannot figure out how to pass this sucker some variables.. 

Like, i just wanna get a callback from 204080 8080a0 404060 a0a0a0 606080 c0c0c0 a0a0a0 606060 808080 404040 c0e080 a0e060 80c020 e0f0a0 a0e040 202020 60a0e0 60c0f0 a0a0a0 a0e0f0 202020 606060 a0a0a0 404020 604020 f0c040 202020  but can't seem to twist its arm..  The -e option looked promising, but to no avail.

There are SOO many weird, niche tasks javascript has mutated to handle over the years, it would be great to hand them off to this guy.. The figurative trojan horse being that it's probably already installed, maybe even runnable - on potential client machines.  As ubiquitous as this thing is though, the documentation is as sparse as the attendance at a Steve Ballmer fan-club meeting...

That said, lol, one of the only semi-useful snippets of info on this JSC was from a posting by an MS employee, from 7 years ago, that suggested... with the title "Commandline.js"

import System;
// This function reads the commandline arguments and prints them
function PrintCommandLineArguments() {
    var args:String[] = System.Environment.GetCommandLineArgs();
    var iValue:int;
// Print the command-line arguments    
for (iValue in args)
    Console.WriteLine(args[iValue]);
}
PrintCommandLineArguments(); 

I couldn't get that to work, but there has to be a way, smarty pants'... Oh and frankly, this just adds to my confusion over the frothing at the mouth that has taken hold of all the server-side JS people of late, as this thing is decidedly old news...  Why was this runtime environment poo-pooed in favor of the current hot-topic solutions, anyways?  Does JSC suck?  Clue me in, sister girlfriends.  ∀Ⓛ∃✖


參考解法

方法 1:

The following seems to work:

# using Bash on Mac OS X 10.6.7
sudo ln -is /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin

# simple example to print passed arguments
jsc --help   # Usage: jsc [options] [files] [-- arguments]
jsc <(echo 'print(arguments[0]); print(arguments);') -- one two three


# http://chir.ag/projects/ntc/
curl -L -O http://chir.ag/projects/ntc/ntc.js
echo '
for(var i in arguments) {
   var n_match = ntc.name(arguments[i]);
   print(n_match);
}
' >> ntc.js

jsc ntc.js -- 204080 8080a0 404060 a0a0a0 606080 c0c0c0 a0a0a0 606060 808080 \
              404040 c0e080 a0e060 80c020 e0f0a0 a0e040 202020 60a0e0 60c0f0 \
              a0a0a0 a0e0f0 202020 606060 a0a0a0 404020 604020 f0c040 202020


# For more information on using javascript from the command line see, for example: 
# - http://www.phpied.com/javascript-shell-scripting/
# - http://littlecomputerscientist.wordpress.com/2008/12/19/command-line-scripting-with-javascript/
# - http://littlecomputerscientist.wordpress.com/2008/12/20/improving-spidermonkeys-load-for-command-line-javascript/

(by Alex Graypete)

參考文件

  1. How to pass an argument to JavaScriptCore/Console via terminal, or otherwise? (CC BY-SA 3.0/4.0)

#javascript #command-line #PHP #webkit #bash






相關問題

為什麼我不能在 IE8 (javascript) 上擴展 localStorage? (Why can't I extend localStorage on IE8 (javascript)?)

在 Javascript 中打開外部 sqlite3 數據庫 (Open external sqlite3 database in Javascript)

Javascript:數組中的所有對像都具有相同的屬性 (Javascript: All Objects in Array Have Same Properties)

為什麼我們要在 javascripts 原型中添加函數? (Why do we add functions to javascripts prototype?)

顯示 URL javascript 的最後一部分? (Display the last part of URL javascript?)

Javascript XMLHttpRequest:忽略無效的 SSL 證書 (Javascript XMLHttpRequest: Ignore invalid SSL Certificate)

有沒有辦法測試 console.log 整體 (Is there a way to test for console.log entires)

如何從 javascript 對像中獲取名稱和值到新列表中? (How to get name and values from a javascript object into a new list?)

數據未發布..幫助!html,js,firebase (Data not posting.. Help! html,js,firebase)

使用 Node.js 腳本查看表單數據 (Seeing form data with Node.js script)

使用百分比查找範圍內的值 (find the value within a range using percent)

如何通過 react.js 中的組件傳遞變量或數據? (How to pass varible or data through components in react.js?)







留言討論