{
"name" : "FastDownload",
"version" : "1.0.1",
"description" : "Get file download link fo Point",
"background" : { "scripts": ["background.js"] },
"permissions" : [
"nativeMessaging",
"contextMenus",
"downloads",
"tabs",
"http://*/*",
"https://*/*"
],
"minimum_chrome_version" : "6.0.0.0",
"manifest_version": 2
}
//Author: match.yangwanqing
//Date: 2014.3.12
//Description: This is a javaScript file use for handle contextMenus action
//When click the contextMenus, it will sent the infomation to native app
//connect to native app
var port = null;
var nativeHostName = "fastdownload";//這個后面會講,是chrome與本地程序通信的橋梁
var downloadID = null;
function getDownloadID(URL)
{
/*chrome.downloads.download({"url": URL},
function(tmpDownloadID)
{
downloadID = tmpDownloadID;
});*/
//chrome.downloads.cancel({"downloadid": downloadID});
chrome.downloads.search({"url": URL},
function(tmpID)
{
alert(tmpID[0].id +
"#" + tmpID[0].totalBytes +
"#" + tmpID[0].mime);
});
}
//onNativeDisconnect
function onDisconnected()
{
//alert("連接到FastDownload服務失敗: " + chrome.runtime.lastError.message);
port = null;
}
//connect to native host and get the communicatetion port
function connectToNativeHost()
{
port = chrome.runtime.connectNative(nativeHostName);//根據配置文件連接到本地程序
port.onDisconnect.addListener(onDisconnected);
}
//調用connectToNativeHost函數連接到本地程序,完成后使用port.postMessage函數即可
//將信息寫入I/O流與本地程序通信
function getClickHandler() {
return function(info, tab) {
connectToNativeHost();
//getDownloadID(info.linkUrl);
port.postMessage(info.linkUrl);
};
};
//在瀏覽器啟動時即創建右鍵菜單,當點擊使用Point下載的時候就會調用getClickHandler()函數處理
//信息並與本地程序通信
chrome.contextMenus.create({
"title" : "使用Point下載",
"type" : "normal",
"id": "FastDownloadMenu",
"contexts" : ["link"],
//"targetUrlPatterns":["*://*/*.*"],
"enabled": true,
"onclick" : getClickHandler()
});
{
"name": "fastdownload",
"description": "Chrome sent download url to native app.",
"path": "/home/yang/QtWork…wnloadPopup/build/home/yang/QtWork…wnloadPopup/build/FastDownloadPopup",
"type": "stdio",
"allowed_origins": [
"chrome-extension://iebejdppbfamhgbnpoacblnlhpllanfbs/"
]
}
unsigned int length = 0;
//read the first four bytes (=> Length)
//getwchar: receive char from stdin
//putwchar: write char to stdout
for (int i = 0; i < 4; i++)
{
length += getwchar();
}
//read the json-message
fileURL = "";
for (int i = 0; i < length; i++)
{
fileURL += getwchar();
}
//瀏覽器端傳來的數據會有一個雙引號引在兩端
fileURL = fileURL.mid(1,fileURL.length()-2);
這段代碼可以放在main函數中也可以放在其他函數中,發揮你的想象吧!當然讀取的方式不止這一種,具體需求因人而異