summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/samples/password_maker/toolstrip.html
blob: a641ff7144176a34aecabae83906a1223b7ed364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<script>
var windowId = null;  // the browser window this toolstrip is in
var currentPort = null;  // the port of the currently focused tab

chrome.windows.getCurrent(function(win) {
  // I think this is guaranteed to be called before our onConnect handler,
  // because we asked for it first.
  windowId = win.id;
});

chrome.self.onConnect.addListener(function (port) {
  if (port.tab.windowId != windowId)
    return;
  port.onMessage.addListener(function (msg, port) {
    if (msg.queryPassword) {
      // A page is requesting a site password.
      openDialog(port);
    } else if (typeof(msg.canQueryPassword) != "undefined") {
      currentPort = port;
      setHasPassword(msg.canQueryPassword);
    }
  });
});

chrome.tabs.onSelectionChanged.addListener(function(tabId, data) {
  // I think it's safe to assume we get this notification before a message
  // from the content script about the 'focus' event.
  if (data.windowId != windowId)
    return;
  currentPort = null;
  setHasPassword(false);
});

function openHelp() {
  window.open('help.html', 'PasswordMakerHelp', 'width=800, height=600');
}

function openDialog(port) {
  height = Math.min(900, screen.availHeight);
  var dialog = window.open('passwordmaker.html', 'PasswordMaker',
                           'width=800, height=' + height);
  dialog.contentURL = port.tab.url;
  dialog.contentPort = port;
}

// ui stuff
function setHasPassword(val) {
  var visible = val ? 'hasPassword' : 'notHasPassword';
  var invisible = !val ? 'hasPassword' : 'notHasPassword';
  document.getElementById(visible).style.display = '';
  document.getElementById(invisible).style.display = 'none';
}

function onClick() {
  if (currentPort) {
    openDialog(currentPort);
  } else {
    openHelp();
  }
}

</script>

<body>
<div class="toolstrip-button" onclick="onClick()">
  <img src="favicon.ico">
  <span id="hasPassword" style="display:none;">Fill Password</span>
  <span id="notHasPassword">PasswordMaker</span>
</div>
</body>