[英]Chrome App add click events on button/input inside the Webview
我制作了这个chrome应用程序,该应用程序使用webview从特定链接加载内容。 我的问题是我无法弄清楚如何向内部元素(例如按钮)添加点击或其他事件。 我需要使用webview,因此在扩展程序中构建内容不是一种选择。 我还需要与webview元素进行通信/更改,因为我需要从USB设备获取数据并在Webview中填充一些输入。
我要么尝试了所有发现的运气,要么运气不佳。
谢谢!
这是我包装中的index.html
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="app.css">
<link rel="stylesheet" href="reset.css">
<script src="jquery.js"></script>
</head>
<body>
<webview id="webview" partition="persist:googlepluswidgets" style="width: 100%; height: 100%;" class="full trim" src="http://urlOfApplication.com" ></webview>
<script src="main.js"></script>
<script src="port.js"></script>
<script src="printer.js"></script>
</body>
Manifest.json
{
"name": "Tezaur - LOCAL",
"version": "1.0.4",
"description": "Tezaur Amanet App",
"permissions": ["webview", "serial","usb","videoCapture","fullscreen", "storage"],
"minimum_chrome_version": "23",
"manifest_version": 2,
"icons": {
"16": "gold-16.png",
"128": "gold-128.png"
},
"app": {
"background": {
"scripts": ["background.js"]
}
}
}
background.js
chrome.app.runtime.onLaunched.addListener(function() {
// Center window on screen.
var screenWidth = screen.availWidth;
var screenHeight = screen.availHeight;
chrome.app.window.create('popup.html', {
// 'id': 'amanet-app',
'state' : 'maximized',
'resizable' : true,
'alwaysOnTop' : false,
state: 'maximized'
},
function(win) {
win.maximize();
// win.clearAttention();
// win.restore();
}
);
});
chrome.app.window.onClosed.addListener(function() {
// Do some simple clean-up tasks.
$.ajax({url:"http://tezaur-local/auth/logout", async:false});
//var expires = new Date();
// expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
//document.cookie = 'laravel_session=;path=/;expires=' + expires.toUTCString();
});
//chrome.app.window.canSetVisibleOnAllWorkspaces(false);
一种方法是使用webview.executeScript在webview
内运行内容脚本 ,因为它可以访问webview
的DOM。
例如:
document.querySelector('webview').executeScript({
code: 'document.getElementById("button-id").onclick = function() { /* click handler */ }',
function(results) { if (results && results.length) { /* successful script injection */ }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.