在 cordova inappbrowser 中添加按钮以隐藏它编辑:并通过 Javascript 添加图像到 inappbrowser
Add button in cordova inappbrowser to hide it EDIT: and add image via Javascript to inappbrowser
我的 Ionic-v1 应用程序中有一个外部 link 到 url 的地图。目前我的 InAppBrowser 中的关闭按钮已激活,我可以再次从我的应用程序中打开 URL。不过地图上的位置当然是不记得了(只是重新打开url)。
所以我在文档中找到了 InAppBrowser.hide() 这对我很有帮助。但是,我找不到在应用程序中添加此方法的方法。什么是最好的方法?
将当前关闭按钮更改为隐藏而不是关闭(因此分别为 Android en iOS 操作 inappbrowser.java en inappbrowser.m
在加载时将 javascript 添加到 inappbrowser 并在此处设置一个隐藏按钮,然后停用关闭按钮
要么…?
有没有人有建议/最佳实践/代码示例?
谢谢!
编辑:我使用了@NickyTheWrench 的解决方案,但想将按钮样式设置为右侧带有徽标(不可点击)的栏。所以我在代码中使用了:
var menu = document.createElement('div');
menu.style = 'height:24px;width:100%;background-color:#fdce0c;font-
size:16px;text-align:left;top:0;left:0;position:fixed;';
document.body.appendChild(menu);
var button = document.createElement('Button');
button.innerHTML = '≡';
button.style = 'height:24px;border:0px;font-size:16px;border-radius:0px;background-color:#fdce0c;';
menu.appendChild(button);
var image = document.createElement('Img');
image.src = 'http://gerhard.technoleno.nl/VGD_transparent_20px.png';
image.style = 'right:0;position:fixed'
menu.appendChild(image);
这在 fiddle: https://jsfiddle.net/m06hv1yt/16/ 中有效,但 ionic cordova 无法提供图像(它使它成为一个带有问号的蓝色框。当我在本地保存图像时,同样的问题是有。如何给这块Javascript添加图片?
编辑 2:编辑答案:url 必须是 https,否则 ionic cordova 找不到它。
是的,这可以使用 addEventListener
和 executeScript
。
查看此代码示例,我们在其中注入 JavaScript,它将在 inappbrowser 的页面顶部生成一个 "Hide Map" 按钮。单击此按钮时,将在 localStorage 中设置一个新项 'hidden',值为 'yes'。然后我们有一个循环来检查该值是否为是,并将隐藏 inappbrowser。
var ref = window.open('https://www.examplemap.com/', '_blank', 'transitionstyle=fliphorizontal,location=no,toolbarposition=top,closebuttoncaption=X');
// Once the InAppBrowser finishes loading
ref.addEventListener("loadstop", function() {
// 1st Clear out 'hidden' in localStorage for subsequent opens.
// 2nd Create the button
ref.executeScript({
code: "var key = 'hidden'; var keyval = 'yes'; localStorage.setItem('hidden',''); var button = document.createElement('Button'); button.innerHTML = 'Hide Map'; button.style = 'top:0;right:0;position:fixed;'; document.body.appendChild(button); button.setAttribute('onclick','localStorage.setItem(key,keyval);');"
});
// Start an interval
var loop = setInterval(function() {
// Execute JavaScript to check if 'hidden' is 'yes' in the
// child browser's localStorage.
ref.executeScript({
code: "localStorage.getItem( 'hidden' )"
},
function(values) {
var hidden = values[0];
// If 'hidden' is equal to 'yes', clear the interval and hide the InAppBrowser.
if (hidden === 'yes') {
clearInterval(loop);
ref.hide();
}
}
);
});
});
另请注意,在某些情况下,隐藏功能对 iOS 不起作用,会显示 "Tried to hide IAB while already hidden"。如果发生这种情况,
希望对您有所帮助:-)
// On your Cordova js
StatusBar.hide();
var ref=window.open('http://www.foo.bar','_blank','zoom=no,location=no,toolbar=no');
ref.addEventListener("loadstop", function() {
ref.executeScript({
code: "localStorage.setItem('close','no');"
});
var loop = setInterval(function() {
ref.executeScript({
code: "try {localStorage.getItem('close');} catch (exception) {}"
}, function(values) {
if (values[0]=== 'yes') {
clearInterval(loop);
ref.hide();
}
});
});
});
// On your external page
$("#exitbutton").on("click",function(e){
window.localStorage.setItem('close','yes');
});
我的 Ionic-v1 应用程序中有一个外部 link 到 url 的地图。目前我的 InAppBrowser 中的关闭按钮已激活,我可以再次从我的应用程序中打开 URL。不过地图上的位置当然是不记得了(只是重新打开url)。
所以我在文档中找到了 InAppBrowser.hide() 这对我很有帮助。但是,我找不到在应用程序中添加此方法的方法。什么是最好的方法?
将当前关闭按钮更改为隐藏而不是关闭(因此分别为 Android en iOS 操作 inappbrowser.java en inappbrowser.m 在加载时将 javascript 添加到 inappbrowser 并在此处设置一个隐藏按钮,然后停用关闭按钮 要么…? 有没有人有建议/最佳实践/代码示例? 谢谢!
编辑:我使用了@NickyTheWrench 的解决方案,但想将按钮样式设置为右侧带有徽标(不可点击)的栏。所以我在代码中使用了:
var menu = document.createElement('div');
menu.style = 'height:24px;width:100%;background-color:#fdce0c;font-
size:16px;text-align:left;top:0;left:0;position:fixed;';
document.body.appendChild(menu);
var button = document.createElement('Button');
button.innerHTML = '≡';
button.style = 'height:24px;border:0px;font-size:16px;border-radius:0px;background-color:#fdce0c;';
menu.appendChild(button);
var image = document.createElement('Img');
image.src = 'http://gerhard.technoleno.nl/VGD_transparent_20px.png';
image.style = 'right:0;position:fixed'
menu.appendChild(image);
这在 fiddle: https://jsfiddle.net/m06hv1yt/16/ 中有效,但 ionic cordova 无法提供图像(它使它成为一个带有问号的蓝色框。当我在本地保存图像时,同样的问题是有。如何给这块Javascript添加图片?
编辑 2:编辑答案:url 必须是 https,否则 ionic cordova 找不到它。
是的,这可以使用 addEventListener
和 executeScript
。
查看此代码示例,我们在其中注入 JavaScript,它将在 inappbrowser 的页面顶部生成一个 "Hide Map" 按钮。单击此按钮时,将在 localStorage 中设置一个新项 'hidden',值为 'yes'。然后我们有一个循环来检查该值是否为是,并将隐藏 inappbrowser。
var ref = window.open('https://www.examplemap.com/', '_blank', 'transitionstyle=fliphorizontal,location=no,toolbarposition=top,closebuttoncaption=X');
// Once the InAppBrowser finishes loading
ref.addEventListener("loadstop", function() {
// 1st Clear out 'hidden' in localStorage for subsequent opens.
// 2nd Create the button
ref.executeScript({
code: "var key = 'hidden'; var keyval = 'yes'; localStorage.setItem('hidden',''); var button = document.createElement('Button'); button.innerHTML = 'Hide Map'; button.style = 'top:0;right:0;position:fixed;'; document.body.appendChild(button); button.setAttribute('onclick','localStorage.setItem(key,keyval);');"
});
// Start an interval
var loop = setInterval(function() {
// Execute JavaScript to check if 'hidden' is 'yes' in the
// child browser's localStorage.
ref.executeScript({
code: "localStorage.getItem( 'hidden' )"
},
function(values) {
var hidden = values[0];
// If 'hidden' is equal to 'yes', clear the interval and hide the InAppBrowser.
if (hidden === 'yes') {
clearInterval(loop);
ref.hide();
}
}
);
});
});
另请注意,在某些情况下,隐藏功能对 iOS 不起作用,会显示 "Tried to hide IAB while already hidden"。如果发生这种情况,
希望对您有所帮助:-)
// On your Cordova js
StatusBar.hide();
var ref=window.open('http://www.foo.bar','_blank','zoom=no,location=no,toolbar=no');
ref.addEventListener("loadstop", function() {
ref.executeScript({
code: "localStorage.setItem('close','no');"
});
var loop = setInterval(function() {
ref.executeScript({
code: "try {localStorage.getItem('close');} catch (exception) {}"
}, function(values) {
if (values[0]=== 'yes') {
clearInterval(loop);
ref.hide();
}
});
});
});
// On your external page
$("#exitbutton").on("click",function(e){
window.localStorage.setItem('close','yes');
});