自动跳转到WebView的某个部分 [英] Automatically jump to a certain part of a WebView
问题描述
我正在制作Chrome应用,并且使用的是与iframe类似的网页视图标记。
有没有办法在Web视图内部的网页上加载一半?
我试过了:
< webview onLoad =window.scroll(0,150)class =testsrc =http:/ /example.co.uk/test.aspx\"></webview>
但看起来这很容易。我甚至没有信服它。
谢谢 解析方案
假设代码执行它不会因为 CSP ),窗口
会引用应用程序的页面,而不是嵌入式页面。
您需要在嵌入式页面的上下文中执行代码。
如果您之前使用过Chrome扩展程序,则会知道与网页内容交互的部分称为 内容脚本 。
应用程序对 < webview>
元素。您可以提前声明哪些页面应该在加载时获得哪些内容脚本,也可以显式加载内容脚本。
窗口。滚动(0,150);
另外,假设您有一个脚本 声明性地,通过调用 明确地说,当页面已经被加载时: app.js
在你的应用程序页面中运行,并且一个变量 webview
是对那里的< webview>
的引用。然后,你可以让webview执行它:
webview.addContentScripts
在之前加载页面(例如在从 app.js
src > code):
//假设< webview id =wv>< / webview>
var webview = document.getElementById(wv);
webview.addContentScripts([{
name:ExampleRule,
matches:[http://example.co.uk/*],//可以和你一样窄希望
js:[content.js]
}]);
webview.src =http://example.co.uk/test.aspx;
//假设< webview id =wvsrc =http://example.co.uk/test.aspx>< / webview>
var webview = document.getElementById(wv);
webview.executeScript({file:content.js});
当然, content.js
更复杂(例如,接收来自应用程序的命令)和/或更精确地控制操作的时间 - 但是这些是通用内容脚本您可以在其他地方找到解决方案或提出新问题的问题。
I'm making a Chrome App, and i'm using the web view tag, which is similar to an iframe.
Is there a way to load halfway down the webpage that's inside the web view?
I have tried:
<webview onLoad="window.scroll(0, 150)" class="test" src="http://example.co.uk/test.aspx"></webview>
But it seems that would be far to easy. I'm not convinced its even possible.
Thanks
Assuming for a moment that code would execute (it won't because of the CSP), window
would refer to the app's page, not the embedded page.
You need to execute the code in the context of the embedded page.
If you worked with Chrome extensions before, you would know that the part interacting with web content is called a Content Script.
Apps have a similar concept for <webview>
elements. You can either declare in advance which pages should get which content script while they load, or you can explicitly load a content script.
So, suppose you have a file content.js
with the following contents (excuse the pun):
window.scroll(0, 150);
Also, assume you have a script app.js
running inside your app page, and a variable webview
is a reference to the <webview>
in there.
Then, you can make the webview execute it:
Declaratively, by calling
webview.addContentScripts
before loading the page (e.g. before settingsrc
fromapp.js
code):// Assuming <webview id="wv"></webview> var webview = document.getElementById("wv"); webview.addContentScripts([{ name: "ExampleRule", matches: ["http://example.co.uk/*"], // can be as narrow as you wish js: ["content.js"] }]); webview.src = "http://example.co.uk/test.aspx";
Explicitly, when the page is already loaded:
// Assuming <webview id="wv" src="http://example.co.uk/test.aspx"></webview> var webview = document.getElementById("wv"); webview.executeScript({file: "content.js"});
It is, of course, possible to make content.js
much more sophisticated (for example, receive commands from the app) and/or more precisely control the timing of your operation - but those are at this point generic content script questions for which you can find solutions elsewhere or ask a new question.
这篇关于自动跳转到WebView的某个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!