以åãChromeæ¡å¼µæ©è½ã§FileSystemAPIã使ãè¨äºãæ¸ãã¾ããã
Chrome拡張機能でFileSystemAPIを使ってみた - メモ的な思考的な
ããã使ã£ã¦å人çãªæ¡å¼µæ©è½ãä½ã£ã¦ããã®ã§ããããµã¨å
æ¥æ°ã¥ããã¨ãããåä½ãã¦ããªããã¨ãåããã¾ããã
ããã§ãç¾å¨ã®Chrome24ã«å¯¾å¿ã§ããããã«ãã¦ã¿ãã¨ãã®ã¡ã¢ãæ®ãã¾ãã
ãä¸è©±ã«ãªã£ããµã¤ã
ManifestFileã®ãã¼ã¸ã§ã³2対å¿ããAPIã®åæ¿ãæ¡å¼µæ©è½ã®ãããã°ãªã©ã§ä»¥ä¸ã®ãµã¤ããåèã«ãªãã¾ããããããã¨ããããã¾ããã
ä¿®æ£ç®æ
1. manifest.jsonã«ã¤ãã¦
manifestã®ãã¼ã¸ã§ã³ã追å ãã¾ãã
"manifest_version": 2,
backgroundé¢ä¿ã®è¨è¼ãå¤æ´ã¨ãªãã¾ãããJavaScriptãæå®ãã¦ãåä½ããããã§ãã
ãã®ãããbackground.htmlå
ã®
ãªããä¸è¦ãªbackground.htmlãã¡ã¤ã«ã¯åé¤ãã¾ããã
- "background_page": "background.html", + "background": { + "persistent": true, + "scripts": [ + "background.js" + ] + },
2. ContentScriptã¨Backgroundéã®ã¡ãã»ã¼ã¸é£æº
ä»ã¾ã§ä½¿ç¨ãã¦ãããchrome.extension.sendRequest()ããchrome.extension.onRequest.addListener()ãã¯éæ¨å¥¨æ±ãã«ãªãã¾ããã
代ããã«ããchrome.extension.sendMessage()ããchrome.extension.onMessage.addListener()ãã«ã¦é£æºããããã«ãªãã¾ããã
3. WebKitBlobBuilderã使ç¨ãããã¡ã¤ã«ä½æã¯ä¸å¯ã«
代ããã«Blobã®ã³ã³ã¹ãã©ã¯ã¿ã使ç¨ãã¦ä½æããããã«ãªãã¾ããã
(WebKitBlobBuilderã使ããã¨ããã¨ããUncaught ReferenceError: WebKitBlobBuilder is not defined ãã¨ã©ã¼)
åèï¼WebKitBlobBuilderが削除 - fragmentary
ãªããBlobã¯appendãæã£ã¦ããªããããå¿
è¦ãªãã®ãã³ã³ã¹ãã©ã¯ã¿ã«ã¦ãã¹ã¦æ¸¡ãã®ãè¯ãããã§ãã
(appendã使ããã¨ããã¨ãããUncaught TypeError: Object #
åèï¼BlobBuilderインターフェースが非推奨となっていた - 人生が二度あれば
ã½ã¼ã¹ã³ã¼ã
3ãã¡ã¤ã«ã ããªã®ã§ã以ä¸ã«è¨è¼ãã¾ãã
GitHubã«ãã¢ãããã¾ããã
thinkAmi/Sample_FileWriter · GitHub
manifest.json
{ "name": "File Writer Sample", "version": "2", "manifest_version": 2, "background": { "persistent": true, "scripts": [ "background.js" ] }, "content_scripts": [ { "matches": [ "http://www.w3.org/" ], "js": [ "contentscript.js" ] } ] }
background.js
// DOM解æ var title = document.getElementsByTagName("TITLE").item(0).firstChild.nodeValue; // URLåå¾ var url = location.href; chrome.extension.sendMessage({ "siteTitle": title, "siteUrl": url });
contentscript.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){ var errorCallback = function(e){}; webkitRequestFileSystem(TEMPORARY, 1024*1024, function(fileSystem){ fileSystem.root.getFile("testfile.txt", {'create':true}, function(fileEntry){ fileEntry.createWriter(function(fileWriter){ // ãã¡ã¤ã«ã®æ¸ãè¾¼ã¿ä½ç½®ã¯ãä¸çªæå¾ã¨ãã fileWriter.seek(fileWriter.length); // åºåè¡ var lines = ''; // 0ãã¤ããã¡ã¤ã«ã®å ´åããããè¡ãä½æãã if (fileWriter.length == 0) { var headers = new Array(addQuote("ãµã¤ãã¿ã¤ãã«"), addQuote("URL")); lines = headers.join(",") + "\n"; } // ãã¼ã¿è¡ã®ä½æ var details = new Array(addQuote(request.siteTitle), addQuote(request.siteUrl)); lines += details.join(",") + "\n"; var blob = new Blob([ lines ], { type: 'text/plain' } ); fileWriter.write(blob); fileWriter.onwriteend = function(e) { console.log('Write completed.'); }; fileWriter.onerror = function(e) { console.log('Write failed: ' + e.toString()); }; }, errorCallback); }, errorCallback); }, errorCallback); }); /* CSVãã¡ã¤ã«ç¨ã«ãé ç®ãããã«ã¯ãªã¼ãã§å²ã */ function addQuote(field) { return "\"" + field + "\""; }
ãã¡ã¤ã«ã®ä¿åå
以åã¨å¤ããããä¸è¨ã®ãµããã£ã¬ã¯ããªã®ä¸ã追ã£ã¦ããã¨ããã¡ã¤ã«ãããã¾ãã
%USERPROFILE%\AppData\Local\Google\Chrome\User Data\Default\File System
å ¬å¼ããã¥ã¡ã³ããªã©
æ¥æ¬èªã®æ å ±ãå¤ãå¯è½æ§ããããããå ¬å¼ããã¥ã¡ã³ããèªãã ã»ããè¯ãããã§ããã