- 在任何一個專案的頂層目錄,建立一個名稱為 scripts 或 monkey 的目錄
- 在此目錄下建立副檔名為 *.js 或 *.em 的 JavaScript 的文件
一個空白文件的內容如下:
/*
* Menu: Samples > Execute Snippet
* Key: M1+M2+M3+F
* Kudos: Jax Hu
* License: EPL 1.0
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
*/
function main(){
}
快捷鍵的代號對應:
M1 | Control/Command |
M2 | Shift |
M3 | Alt/Option |
一個可以用來顯示物件成員的函數:
/**顯示物件的成員
* @param {Object} val 物件
*/
function var_dump(val){
var name, value, temp=[];
for (name in val) {
try {
value = (val[name]+'').
replace("function",'<b style="color:#00f;">$&</b>');
}catch (e) {
value = (e+'').fontcolor('red');
}
temp.push('<b style="color:#707;">'+name+'</b> = '+value);
}
webView = views.getView("var_dump");
webView.showView(true);
webView.setTitle('var_dump');
webView.setHTML(temp.join("<hr/>").fixed());
}
常用方法以及數值:
/*當前文件的位置
* => D:/WorkSpace/my_project/test.js */
location
/*當前文件的名稱
* => test.js */
editors.activeEditor.title
/*當前的文件內容*/
editors.activeEditor.source
/*當前文件的 URI 位址*/
editors.activeEditor.uri
/*儲存當前文件*/
editors.activeEditor.save();
/*對當前文件-開啟另存新檔的對話匡*/
editors.activeEditor.textEditor.doSaveAs()
/*對當前文件的目錄路徑
* => D:/WorkSpace/my_project */
editors.activeEditor.textEditor.editorInput.file.parent.location
/*目前開啟的所有文件*/
editors.all[];
/*儲存全部編輯器,傳入 true 會開啟存檔提示*/
window.workbench.saveAllEditors(false);
/*重新開啟 Eclipse*/
window.workbench.restart();
/*關閉 Eclipse*/
window.workbench.close();
/* 開新的編輯器,可以透過這個開啟空白文件,或是已經存在的檔案,
* 之後 editors.activeEditor 會轉到這個文件上 */
fileUtils.open('textile_to_redmine.txt');
/*取得當前的專案名稱*/
editors.activeEditor.textEditor.editorInput.file.project.name;
/*取得所有的專案*/
Packages.org.eclipse.core.resources.ResourcesPlugin.workspace.root.projects;
editors.activeEditor.textEditor.editorInput.file.workspace.root.projects
建立一個新的視圖,或開啟已存在的視圖:
webView = views.getView("my_view_name");
/*顯示視圖*/
webView.showView(true);
/*設定標題*/
webView.setTitle("My View Title");
/*設定內容的HTML*/
webView.setHTML('<h1>OK</h1>');
/*或指定內容的網址*/
webView.url = "http://www.google.com";
webView.addEventListener("LocationChanging", function(event){
var location = event.innerEvent.location;
// Print out the location to the Java console
Packages.java.lang.System.out.println("You clicked on: " + location);
});
替換選擇的文字區段:
/*選擇的起始位置*/
var starting = editors.activeEditor.selectionRange.startingOffset;
/*選擇的結束位置*/
var ending = editors.activeEditor.selectionRange.endingOffset;
/*選擇的文字內容*/
var text = editors.activeEditor.source.substring(starting, ending);
/*文字跳脫處理,或其他自訂的處理*/
text = escape(text);
/*替換選擇的文字*/
editors.activeEditor.applyEdit(starting, ending-starting, text);
/*重新選擇文字區段*/
editors.activeEditor.selectAndReveal(starting, text.length);
檔案存取:
var file = new File("myFile.txt");
file.createNewFile();
file.write("Date: ");
var text = file.readLines();
Web 資料請求的方式:
var req = new WebRequest();
req.open("GET", "http://xml.weather.yahoo.com/forecastrss?p=94103");
var text = req.send();