雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

chrome extensionで現在タブで開いているページのタイトルとurlを取得

以下のコードを同じフォルダにいれる。

基本的に、最初のサンプルプログラムであるhello サンプルを変更しただけ。

manifest.json

{
  "name": "Hello Extensions",
  "description" : "Base Level Extension",
  "version": "1.0",
  "browser_action": {
    "default_popup": "hello.html",
    "default_icon": "hello_extensions.png"
  },
  "permissions": [
    "tabs"
  ],
  "manifest_version": 2,
  "commands": {
    "_execute_browser_action": {
      "suggested_key": {
        "default": "Ctrl+Shift+F",
        "mac": "MacCtrl+Shift+F"
      },
      "description": "Opens hello.html"
    }
  }
}

hello.html

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="main.js"></script>
</head>
<body style="min-width:250px">
<div id="title"></div>
<div id="url"></div>
</body>
</html>

main.js

window.onload = function() {
  chrome.tabs.getSelected(null, function(tab) {
    document.getElementById('title').innerHTML = tab.title;
    document.getElementById('url').innerHTML = tab.url;
    // $B$=$N$^$^Aw?.(B
    var form = document.createElement('form');
    var request = document.createElement('input');
    form.method = 'POST';
    form.action = 'http://0.0.0.0:8000/';
    request.type = 'hidden'; //$BF~NO%U%)!<%`$,I=<($5$l$J$$$h$&$K(B
    request.name = tab.title;
    request.value = tab.url;
    form.appendChild(request);
    document.body.appendChild(form);
    form.submit();  

  });
};