12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

初投稿:ChromeAppでの基本的な知識1(Window, Event周り)

Posted at

ChromeAppsでの基本的なことのメモ。

1 イベントの設定

イベントは手軽に

onxxx="xxxx_clicked();"

などとやりたくなるけど、ChromeAppsでは次のようなエラーが出て機能しない。
Refused to execute inline event handler because it violates the following Content Security Policy directive: "default-src 'self' chrome-extension-resource:". Note that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.
イベントを設定するならjavascript側で、

document.getElementById("xxxx").addEventListener("click",xxxx_clicked);

などとするとよい。

2 ウィンドウを開く

新しいウィンドウを開く時、手軽に

window.open("xxx.html",...);

などとやりたくなるけれど、こちらもイベントのときのようにエラーが出てしまう。
やるのであれば、Chrome Platform APIを使って

chrome.app.window.create("xxx.html","id",...);

をする必要がある。なお、chrome.app.windowのAPIを使う前に、
manifest.jsonpermissionapp.window の権限を加えておく。
これで開いたウィンドウを操作する際は、次のようにしてAppWindowオブジェクトを取得する。

var win = chrome.app.window.get("id");
or
var winarr = chrome.app.window.getAll();

なお、 getAll() メソッドだと作成した全てのAppWindowの配列が返ってくる。

3 終わりに

ChromeAppsで使うjavascriptは、通常のjavascriptやFirefoxOSなどで当たり前に使っていた作法と
若干異なる場合ので、気をつけたいところである。

12
10
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
12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?