镀铬扩展,试图最小化托盘按钮

时间:2015-07-29 13:27:32

标签: javascript google-chrome-extension

我正在尝试使用Chrome应用示例来制作具有顶级自定义顶栏的Chrome扩展程序。我可能有屠夫的代码,但有人可以帮助它的工作吗?

enter image description here     https://github.com/GoogleChrome/chrome-app-samples/tree/master/samples/frameless-window

我无法使最小化托盘按钮起作用

function closeWindow() {
  window.close();
}

function minimizeWindow() {
  window.minimize();
}

function updateImageUrl(image_id, new_image_url) {
  var image = document.getElementById(image_id);
  if (image)
    image.src = new_image_url;
}

function createImage(image_id, image_url) {
  var image = document.createElement("img");
  image.setAttribute("id", image_id);
  image.src = image_url;
  return image;
}

function createButton(button_id, button_name, normal_image_url,
                       hover_image_url, click_func) {
  var button = document.createElement("div");
  button.setAttribute("class", button_name);
  var button_img = createImage(button_id, normal_image_url);
  button.appendChild(button_img);
  button.onmouseover = function() {
    updateImageUrl(button_id, hover_image_url);
  }
  button.onmouseout = function() {
    updateImageUrl(button_id, normal_image_url);
  }
  button.onclick = click_func;
  return button;
}

function focusTitlebars(focus) {
  var bg_color = focus ? "#3a3d3d" : "#000000";

  var titlebar = document.getElementById("top-titlebar");

  if (titlebar)
    titlebar.style.backgroundColor = bg_color;
}

function addTitlebar(titlebar_name, titlebar_icon_url, titlebar_text) {
  var titlebar = document.createElement("div");
  var titlebar_name = "top-titlebar";
  titlebar.setAttribute("id", titlebar_name);
  titlebar.setAttribute("class", titlebar_name);

  var icon = document.createElement("div");
  icon.setAttribute("class", titlebar_name + "-icon");
  icon.appendChild(createImage(titlebar_name + "icon", "16.png"));
  titlebar.appendChild(icon);

  var title = document.createElement("div");
  title.setAttribute("class", titlebar_name + "-text");
  title.innerText = document.title; // titlebar name
  titlebar.appendChild(title);

   var win = chrome.app.window.current();

  var closeButton = createButton(titlebar_name + "-close-button",
                                 titlebar_name + "-close-button",
                                 "img/button_close.png",
                                 "img/button_close_hover.png",
                                 closeWindow);
  titlebar.appendChild(closeButton);
  // Create minimize button
  var minimizeButton = createButton(titlebar_name + "-minimize-button",
                                 titlebar_name + "-minimize-button",
                                 "img/button_minimize.png",
                                 "img/button_minimize_hover.png",
                                 minimizeWindow);
  titlebar.appendChild(minimizeButton);
  //

  var divider = document.createElement("div");
  divider.setAttribute("class", titlebar_name + "-divider");
  titlebar.appendChild(divider);

  document.body.appendChild(titlebar);
}

function removeTitlebar(titlebar_name) {
  var titlebar = document.getElementById(titlebar_name);
  if (titlebar)
    document.body.removeChild(titlebar);
}

function updateContentStyle() {
  var content = document.getElementById("content");
  if (!content)
    return;

  var left = 0;
  var top = 0;
  var width = window.outerWidth;
  var height = window.outerHeight;

  var titlebar = document.getElementById("top-titlebar");
  if (titlebar) {
    height -= titlebar.offsetHeight;
    top += titlebar.offsetHeight;
  }


  if (titlebar) {
    width -= titlebar.offsetWidth;
  }

  var contentStyle = "position: absolute; ";
  contentStyle += "left: " + left + "px; ";
  contentStyle += "top: " + top + "px; ";
  contentStyle += "width: " + width + "px; ";
  contentStyle += "height: " + height + "px; ";
  content.setAttribute("style", contentStyle);
}

1 个答案:

答案 0 :(得分:0)

这将解决问题

function minimizeWindow() {
var window = chrome.app.window.current();
  window.minimize(); 

}