如何在浏览器中检测特定的URL并在Javascript中获取HTML页面关联? [英] How detect a specific url in browser and get html page associate in Javascript?

查看:98
本文介绍了如何在浏览器中检测特定的URL并在Javascript中获取HTML页面关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用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.

感谢您对我的帮助

推荐答案

当我输入

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆