google-chrome-extension - Chrome : Webview not resizing

标签 google-chrome-extension google-chrome-app

我正在测试 <webview> Chrome 中的元素,我已经阅读了文档,但我无法弄清楚为什么 Webview在父窗口调整大小时不调整。

Index.Html

<body>
<webview  src="http://website.com" style="width:1010px; height:700px" minwidth="1010" minheight="700" autosize="on""></webview>

<script src="index.js"></script>

background.js

chrome.app.runtime.onLaunched.addListener(function() {
chrome.app.window.create('index.html', {
'bounds': {
  'width': 1010,
  'height': 700
 }});});

Index.js

$(function() {
function resizeWebView() {
    $('webview').get(0).width = $(window).width();
    $('webview').get(0).height = $(window).height();
}
$(window).resize(resizeWebView);
resizeWebView();
});

我也尝试删除 autosize=on按照建议,但它不起作用。 另一个问题是如何禁用主窗口(Embedder)调整大小。 谢谢

最佳答案

你应该使用 chrome.app.window.onBoundsChanged chrome.app.window.create 返回的窗口对象的 API。简单实现:

background.js

var appWin = null;

chrome.app.runtime.onLaunched.addListener(function() {
  chrome.app.window.create(
    'index.html', 
    {'bounds': {'width': 1010, 'height': 700}},
    onWindowCreated
  );
});

function onWindowCreated(win) {
  appWin = win;
  // Resize the webview when the window resizes.
  appWin.onBoundsChanged.addListener(onBoundsChanged);
  // Initialize the webview size once on launch.
  onBoundsChanged();
}

function onBoundsChanged() {
  var webview = document.querySelector('webview');
  var bounds = appWin.getBounds();
  webview.style.height = bounds.height + 'px';
  webview.style.width = bounds.width + 'px';
}

index.js

// Nothing here

在此处查看一个更详尽的面向对象的示例,其中包含多个窗口并保留/重复使用用户设置的最后一个窗口大小:https://github.com/GoogleChrome/chrome-app-samples/tree/master/url-handler .

关于你的第二个问题,如果你单独问它会很好,但是:只需设置 chrome.app.window.createresizable 参数即可。为假:

  ...
  chrome.app.window.create(
    'index.html', 
    {
      'bounds': {'width': 1010, 'height': 700}, 
      'resizable': false
    },
  ...

关于google-chrome-extension - Chrome : Webview not resizing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20043661/

相关文章:

javascript - chrome丰富的通知更改超时关闭

javascript - (Chrome 扩展)在页面 addEventListener 之前从扩展执行某些操作

css - 我的 CSS 没有通过我的内容脚本注入(inject)

google-chrome-app - 自动签署谷歌帐户 chrome 应用程序

css - 我需要将所有前缀添加到 chrome 桌面应用程序吗?

javascript - 谷歌浏览器应用程序开发是否允许您将 html 和 javascript 添加到本地存储的文件中,具体取决于用户

google-chrome-extension - 使用 chrome.tabs.onUpdated 时忽略本地 URL

javascript - 如何限制用户在 Chrome 扩展选项中输入 float

javascript - 维护 Chrome 打包应用程序的登录状态

google-chrome - 如何检测 Chrome 中是否安装了 Google Cast 扩展程序?