LoginSignup
47
45

More than 5 years have passed since last update.

ChromeアプリをAngularJSで書くまでの手順

Posted at

前提

手順①:Chromeアプリ雛型の作成

  • (まだインストールしてなければ)Chromeアプリの雛型ジェネレータをインストール
# npm install -g generator-chromeapp
  • 雛型ジェネレータを実行
$ mkdir chrome-test
$ cd chrome-test
$ yo chromeapp
  • デフォルトのまま進行

    • 実際は色々パーミッションを設定する必要があるが、後でも変更できるので今回はスルー(全てそのまま[ENTER]キー)

      スクリーンショット 2014-04-29 23.25.57.png
      スクリーンショット 2014-04-29 23.26.36.png
      スクリーンショット 2014-04-29 23.27.28.png
      スクリーンショット 2014-04-29 23.27.42.png

  • 設定したパーミッションの情報は app/manifest.json に書かれてる

    app/manifest.json
    {
        "name": "__MSG_appName__",
        "description": "__MSG_appDescription__",
        "version": "1",
        "manifest_version": 2,
        "default_locale": "en",
        "permissions": [],
        "icons": {
            "16": "images/icon-16.png",
            "128": "images/icon-128.png"
        },
        "app": {
            "background": {
                "scripts": [
                    "scripts/main.js",
                    "scripts/chromereload.js"
                ]
            }
        }
      }
    

    補足:後からパーミッションを変更したければ、

    $ yo chromeapp:permission
    
  • 一応、これで最低限は動くはずなので、Chromeで確認してみる。Chromeの拡張機能メニューを開き、

    スクリーンショット 2014-04-29 23.47.17.png

  • 雛型の app ディレクトリを選択

    スクリーンショット 2014-04-29 23.49.01.png

  • 先ほどの雛型のChromeアプリが読み込まれるので、

    スクリーンショット 2014-04-29 23.49.28.png

  • 起動する

    スクリーンショット 2014-04-29 23.50.50.png

  • この時点で、Macのアプリ一覧にも追加されている。

    スクリーンショット 2014-04-30 0.04.24.png

  • 以下はメモ。

    • Chromoアプリとして動作確認(ファイルの変更を監視&反映してくれる)

      $ grunt debug
      
    • 上記を通常のブラウザで動作確認する場合

      $ grunt debug:server
      
    • 本番向けのミニファイされたソースの作成

      $ grunt build
      

      すると dist ディレクトリにファイルが作成される

    • Chromeでの配布用パッケージの作成

      スクリーンショット 2014-04-29 23.47.17 のコピー.png

    • ロゴ画像は app/manifest.json で定義されているので変えたければ変える

手順②:Chromeアプリ雛型にAngularJS組み込み

  • Bower で AngularJS をインストール

    $ bower install angular --save
    
  • 本当は AngularJS を MVC?の構成にしたいところだが、今回は省略。app/index.html を編集

    app/index.html
    <!doctype html>
    -<html>
    +<html ng-app ng-csp>
        <head>
            <meta charset="utf-8">
            <title>chrome test</title>
    +        <link rel="stylesheet" href="bower_components/angular/angular-csp.css">
            <!-- build:css styles/main.css -->
            <link rel="stylesheet" href="styles/main.css">
            <!-- endbuild -->
    +        <script src="bower_components/angular/angular.js"></script>
        </head>
        <body>
            <h1>'Allo</h1>
    
    +        <div ng-controller="TestCtrl">
    +            {{sample}}
    +        </div>
    
            <!-- build:js scripts/index.js -->
            <script src="scripts/index.js"></script>
            <!-- endbuild -->
        </body>
    </html>
    
  • app/scripts/index.js を編集

    app/scripts/index.js
    'use strict';
    document.addEventListener('DOMContentLoaded', function() {
        var h1 = document.getElementsByTagName('h1');
        if (h1.length > 0) {
            h1[0].innerText = h1[0].innerText + ' \'Allo';
        }
    }, false);
    
    +function TestCtrl($scope){
    +    $scope.sample = "こんにちは世界!";
    +};
    
  • 結果

    スクリーンショット 2014-04-30 1.13.24.png

  • ポイント

    • index.htmlng-csp の記載と angular-csp.css の読み込みが必要らしい
    • なんとなく、function を書く場所が重要そうだ
      • 上記の function を index.htmlhead エリアに書いた場合、Chromeアプリでは AngularJS が動作しなかった
        • 通常のブラウザでは動作した
      • どの作法が正しいかは別として、通常ブラウザとは挙動が異なるみたい

おわりに

また機会があったら続きを検証します。

47
45
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
45