- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个供个人使用的扩展程序,它只会自动重复网站 A 上的某些操作。这基本上是我想使用 Chrome 扩展程序自动化的流程:
目前,我的扩展程序正在为我想要自动化的特定网站使用 Chrome 的 pageAction
。
manifest.json
{
"manifest_version": 2,
"name": "Automater",
"version": "0.0.1",
"page_action": {
"default_title": "Click here!",
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"https://www.website.com/*"
],
"js": ["jquery-3.2.1.min.js", "content.js"],
"run_at": "document_end"
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"activeTab",
"tabs",
"declarativeContent",
"storage"
]
}
background.js
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(
activeTab.id,
{ 'message': 'click_btn' }
);
});
});
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(
undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
urlContains: 'www.website.com',
schemes: ['https']
},
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
chrome.windows.onCreated.addListener(function(newWindow) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(
activeTab.id,
{ 'message': 'fill_form' }
);
});
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === 'btn_clicked' ) {
citationsSize = request.citationsSize;
}
}
);
content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.message) {
case 'click_btn':
$("#btn_id").click();
chrome.runtime.sendMessage({
"message": 'btn_clicked'
});
break;
case 'fill_form':
console.log('start filling');
};
}
);
我在网站上。单击扩展图标后,$("#btn_id").click();
起作用,它会打开一个新窗口作为弹出窗口(此弹出窗口由我当前所在的网站打开) .我使用 chrome.windows.onCreated
捕捉新打开的窗口,但是,从这里,我无法向新窗口的 fill_form
发送消息内容脚本。
如何在新打开的弹窗中执行脚本?
最佳答案
您可以通过多种方式解决此问题,但以我的拙见,您并不真的需要消息。如果你想在点击页面 Action 时执行内容脚本,那么不要在manifest.json
中添加,而是将其拆分成两个不同的文件,一个用于点击按钮,一个用于点击按钮填写并发送表单,然后在需要时使用 chrome.tabs.executeScript
以编程方式注入(inject)它们。
工作流程如下:
content_click_btn.js
在页面中加载并点击按钮。content_fill_form.js
被注入(inject)弹出窗口。你的代码会变成这样:
background.js
:
chrome.pageAction.onClicked.addListener(function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {file: 'content_click_btn.js', runAt: 'document_end'});
});
});
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
urlContains: 'www.website.com',
schemes: ['https']
},
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}]);
});
});
注意:自 2021 年 1 月起,使用 chrome.action
而不是 chrome.pageAction
和 chrome.scripting.executeScript()
而不是 chrome.tabs.executeScript()
。
content_click_btn.js
:
function listener(newWindow) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs[0].url.includes('http://www.website.com')
chrome.tabs.executeScript(tabs[0].id, {file: 'content_fill_form.js', runAt: 'document_end'});
});
chrome.windows.onCreated.removeListener(listener);
}
chrome.windows.onCreated.addListener(listener);
document.getElementById('btn_id').click();
content_fill_form.js
:
console.log('Filling the form...');
// Do something to fill and send the form.
此外,您实际上并不需要它,但如果您想使用 jQuery,您可以将它留在 list 中的 "content_scripts"
字段中。
关于javascript - 将 content_script 注入(inject)新打开的窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552764/
这不是重复,我已经检查过问题 Chrome extension: Modifying the content of a webpage 但它没有回答我的问题,因为我只想在特定时间阻塞,因此阻塞/不阻塞
我正在开发一个扩展程序,希望使其与我的网站进行交互。但是当我尝试调用位于“content_script.js”文件中的函数时,它说该函数未定义! 最佳答案 第 1 步:了解 isolated cont
我正在尝试制作一个 Chrome 扩展程序,它将在我的 Gmail 中显示一个弹出窗口,其中包含来 self 的外部服务器的一些信息,例如今天的天气。 我尝试过的 我尝试将 js 包含在 chrome
我有这个简单的扩展,它在 chrome 的工具栏上显示图标并显示启用/禁用按钮。我想向按钮添加功能以禁用或启用在访问谷歌网站时触发的 content_script.js: 弹出窗口 var setLa
我有一个简单的 manifest.json,但出于某种原因,我一直遇到这个错误 无法为内容脚本加载 JavaScript“content_script.js” 一旦我添加 content_script
我对制作 Chrome 扩展程序很陌生,我会尝试一些东西: 我将把 JS 变量从“browser_action”发送到“content_scripts” "browser_action": {
我正在开发 Chrome 扩展,我需要将 javascript 文件加载到内容脚本,但该文件是通过 webpack-dev-server 提供的。所以它只能在本地主机上使用。 我尝试更改我的 mani
最初在这里发布这个问题: https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/wbSpXvnO10A/nov36skm
我刚刚开始使用 JavaScript,正在尝试修改 MDN 教程中的脚本,Your First WebExtension 我正在尝试在网页周围绘制红色或蓝色框,具体取决于它是 http://还是 ht
我认为这是一个经常被问到的问题,但我找不到答案。有没有办法让 Chrome 扩展程序使其浏览器操作弹出窗口仅在 content_scripts 匹配页面上可用? 例如:我为一个网站做了一个扩展。我希望
最近,我制作了一个 Chrome 扩展来强制所有网页使用给定的字体。它曾经工作得很好,但从昨天(也许更早)开始就停止工作了。 扩展的关键代码是: manifest.json file content:
我正在编写一个包含 content_script 的小型 Chrome 扩展程序。 它将在单个域上运行,我正在尝试稍微改进一个站点。 我想在我的内容脚本中使用 jQuery,但是网站也使用 jQuer
我有一个测试 Chrome 扩展程序,运行于 。它运行一个仅写入域的 content_script。 它在所有情况下都工作正常,除非我编写不存在的域。 扩展的目的是在特定域上运行,无论发生什么情况。
我正在开发一个供个人使用的扩展程序,它只会自动重复网站 A 上的某些操作。这基本上是我想使用 Chrome 扩展程序自动化的流程: 触发按钮点击事件。 打开一个新的弹窗(由网站A打开)。 在打开的窗口
我想将一个已保存的变量从内容脚本传递到弹出页面,这样我就可以简单地输出它。例如,如果我的内容脚本具有以下代码: var x = 'abc'; 我希望弹出窗口标题如此可变。 最佳答案 有几种方法可以做到
我是 javascript 的新手,正在尝试学习 Chrome 扩展程序。我搜索了我能想到的问题的所有变体,但我找到的答案不适用于我的特定问题。我已经阅读了 chrome 扩展的教程,但我认为应该有效
如何更改在 content_scripts (manifest.json) 中声明的 CSS 文件? 我需要将它从 background.js 更改(style.css 到 style2.css) 有
我正在尝试学习如何使用 content_script.js 将请求发送到我的 chrome 扩展程序的后台页面。我读过很多关于此的答案,但没有一个对我有帮助。我似乎无法让监听器执行。 Manifest
到目前为止,我还没有在我的 Firefox 中安装任何插件,也没有使用任何额外的 JS 脚本,但我没有从启用此 content_script.js 的地方打印不必要的消息像这样的控制台: conte
我正在尝试从外部服务器加载 JS,以便可以更新和开发我的扩展程序,而无需更新 chrome 扩展程序。这是我目前的 list : { "name": "Test", "version": "1
我是一名优秀的程序员,十分优秀!