如何在浏览器中检测特定的URL并在Javascript中获取HTML页面关联? [英] How detect a specific url in browser and get html page associate in Javascript?
问题描述
我需要用javascript
修改html
页面(填写字段).
我有一个特定的URL,但是当浏览器位于正确的URL上时,我不知道如何收听并获得html
代码关联.
I need to modifiy a html
page with javascript
(fill a field).
I have a specific url but i do not know how to listen when the browser is on the right url and get the html
code associate.
我使用web扩展名进行操作,因此javascript
必须在Web浏览器中检查url并从此处获取html
.
I do it with a webextension, so the javascript
has to check url in the web browser and get html
from here.
感谢您对我的帮助
推荐答案
当我输入来自MDN的本教程.非常接近它的顶部的是这个代码示例:
When I type firefox extension tutorial into Google, the first hit is this tutorial from MDN. Very close to the top of it is has this code example:
现在,在文件的正下方直接创建一个名为"manifest.json"的新文件 "borderify"目录.给它提供以下内容:
Now create a new file called "manifest.json" directly under the "borderify" directory. Give it the following contents:
{
"manifest_version": 2,
"name": "Borderify",
"version": "1.0",
"description": "Adds a red border to all webpages matching mozilla.org.",
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["*://*.mozilla.org/*"],
"js": ["borderify.js"]
}
]
}
说
这里最有趣的键是content_scripts,它告诉Firefox 将脚本加载到URL匹配特定模式的网页中. 在这种情况下,我们要求Firefox加载一个名为 "borderify.js"进入"mozilla.org"提供的所有HTTP或HTTPS页面 或其任何子域.
The most interesting key here is content_scripts, which tells Firefox to load a script into Web pages whose URL matches a specific pattern. In this case, we're asking Firefox to load a script called "borderify.js" into all HTTP or HTTPS pages served from "mozilla.org" or any of its subdomains.
因此,在清单中,将"*://*.mozilla.org/*"
更改为与要在其上运行脚本的页面相匹配的内容.
So in your manifest, change "*://*.mozilla.org/*"
to something which matches the page you want to run your script on.
内容脚本("borderify.js"
)应该能够使用querySelector
之类的标准方法访问该页面的DOM.
The content script ("borderify.js"
) should be able to access the DOM of that page using standard methods like querySelector
.
这篇关于如何在浏览器中检测特定的URL并在Javascript中获取HTML页面关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!