抱歉标题含糊不清,我会尽力具体描述。
如果您查看我在此网站上提出的上一个问题,您会发现我询问了有关如何填充 Chrome 上下文菜单的提示。我使用上下文菜单的原因是我想要一个易于访问的界面,向用户呈现值列表。这些值代表不同类型的文档(例如出生证明或税务报告)。这些文件中的每一个都有独特的属性(例如:出生证明是“出生证明”和“身份”)和规则。当我必须将文件上传到 DMS(文档管理系统)(基本上是一个 ASP.Net 网站)时,将使用这些属性和规则。文本字段和下拉列表用于接收我之前提到的规则和属性。通常,在添加文件时,我必须根据文档的类型手动填写这些字段,这是一个非常耗时的过程。
现在,我想要要做的就是只需单击上下文菜单的一项(同样,假设我单击“出生证明”),页面上的字段将自动显示更新为相应的值。
到目前为止,我所做的是将大部分菜单项/文档类型及其相应的属性/规则放入 JSON 格式。我的初衷是使用上下文菜单扩展中包含的脚本来设置页面字段的值。只是,如果我没有弄错的话(如果我弄错了,请告诉我),你不能直接通过 Chrome 扩展程序修改网页上 HTML 元素的值。
据我了解,用户脚本能够完成该任务,但我仍然希望有一个控制界面(上下文菜单)来选择文档类型(我不想要一个始终使用相同参数执行的脚本) 。
那么,根据我的描述,您认为完成这项任务的最佳方法是什么?我可以“调用”用户脚本或至少传递一些参数,以便我可以根据需要修改文本字段/下拉列表的值吗?非常欢迎任何建议。
PS 我测试了一些脚本,看看这些字段是否真的可以通过编程方式更改其值。我只是将带有“javascript:”前缀的代码(带有一些预定义的字段值)复制粘贴到地址栏中,它就像一个魅力!现在,我只需要
- 1) 能够在不使用此地址栏技巧的情况下执行此操作,
- 2) 有一个实际的界面来选择值,而不是仅仅对它们进行硬编码。
Sorry for the vague title, I'll try to be as specific as I can for the description.
If you look at my previous question on this site, you'll see that I asked for tips on how to populate Chrome context menus. The reason why I am using the context menus is that I want an easy-to-access interface that presents the user with a list of values. Those values represent different types of documents (for example a birth certificate or a tax report). Each of these documents have unique attributes (ex: birth certificate is "Birth certificate" and "Identity") and rules. Those attributes and rules are used when I have to upload the files to a DMS (document management system), which is basically an ASP.Net web site. Text fields and dropdown lists are used to receive the rules and attributes I mentioned earlier. Usually, when adding a file, I have to fill those fields by hand based of the type of document, a very time consuming process.
Now, what I want to do is to simply be able to click on one of my context menu's items (again, let's say I click on "Birth certificate") and the fields on the page will automatically be updated with the corresponding values.
What I have done so far is to put most of the menu items/document types and their corresponding attributes/rules in JSON format. My original intention was to set the values of the page's fields with a script included in the context menu extension. Only, if I'm not mistaken (if I am, please tell me), you cannot modify the value of HTML elements on a web page directly through a Chrome extension.
From what I understand, userscript would be able to fulfill that task, but I still want to have a control interface (the context menu) to choose the type of document (I don't want a script that always executes with the same parameters).
So, from what I have described, what would you say is the best way to achieve this task ? Can I "call" userscript or at least pass some parameter so that I can modify the values of text fields/dropdown lists as I wish ? Any advice is greatly weclome.
P.S. I tested some script to see if the fields could actually have their values changed programatically. I just copy pasted the code (with some pre-defined values for the fields) in the adress bar with the "javascript:" prefix and it works like a charm! Now, I just need to
- 1) Be able to do this without using this address bar trick and
- 2) have an actual interface to chose values instead of just hard coding them.
发布评论
评论(1)
当然可以。查看内容脚本,可以将其设置为自动将其自身注入到以下网站中: 匹配指定模式。您还可以通过
chrome.tabs.executeScript
。 Chrome 扩展程序可以访问的工具远多于用户脚本。
您只需在 创建上下文菜单,然后运行:
加载脚本后,您有多个选项,但我只会使用 消息传递从后台页面获取适当的信息,然后修改字段。
简单示例
manifest.json(将其添加到您的权限中)
script_to_inject.js
background.html(或background.js)
Sure they can. Check out content scripts, which can be set up to inject themselves automatically in sites that match a specified pattern. You can also do programmatic injection via
chrome.tabs.executeScript
. Chrome extensions have access to far more tools than user scripts.You'd just use whatever
onclick
callback you've set when creating your context menu, then you'd run:Once that script loads, you have several options, but I would just use message passing to get the appropriate info from your background page and then modify the fields.
Simple example
manifest.json (add this to your permissions)
script_to_inject.js
background.html (or background.js)