如何填写从chrome扩展的网页上的窗体内的文本字段

How to fill out a textfield within a form on a webpage from a chrome extension?

本文关键字:窗体 文本 字段 网页 chrome 扩展 何填写      更新时间:2023-09-26

到目前为止,我已经得到了一个扩展,列出了网页的用户是在和一个按钮。当点击按钮时,应该用"testing123"填充文本框。

在我测试的网页上,表单有一个id和名称"form",文本字段有一个id和名称"body"。

如果我能得到一些帮助填写这个文本字段或一般文本字段,这将是非常感激。

以下是我目前收集到的文件:

manifest.json

<>之前{"name":扩展菜单;"版本":"1.0","manifest_version":2"description": "My extension"," browser_action ": {"default_icon":"程序","default_menu":"菜单",:"default_popup popup.html"},"图标":{"128":"程序"},"权限":["tabs", "http://*/*", "activeTab"]}之前

popup.html

<p id="currentLink">Loading ...</p>
<hr />
<ul id="savedLinks"></ul>
<script type="text/javascript" src="popup.js"></script><br>
<button type="button" onclick="fill in text box with the word 'testing123'">Go!</button>

popup.js

<>之前chrome.tabs。getSelected(null, function(tab) {//$("#").val("testing123");//. getelementbyid(身体的)。Value = 'testing123';. getelementbyid("currentLink")。innerHTML = tab.url;});之前

我试过使用:

  1. $("#body").val("testing123");

  2. document.getElementById('body').value = 'testing123';

您无法从popup直接访问该网页。您应该使用内容脚本来完成此操作。Content scripts在网页上下文中运行

将此添加到您的manifest:

"content_scripts": [
        {
            "matches": [
                "http://www.example.com/*"
            ],
            "js": [
                "jquery.js",
                "myscript.js"
            ]
        },

myscript.js :

$("#body").val("testing123");
document.getElementById('body').value = 'testing123';