【发布时间】:2020-02-12 04:24:28
【问题描述】:
我只是学习 JavaScript,主要是为了在特定网站上自动搜索并比较从这些搜索中收集的一些数据。因此,我想知道是否可以在我想要创建的 chrome 扩展中使用 JavaScript 在该站点中的搜索栏上做这样的事情。
注意:网站中搜索栏的示例类似于 YouTube's 搜索栏
编辑:经过更多研究,我发现forum.submit 可能有用,但我不太确定。
编辑 2:
extension_ui.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>extension_ui</title>
</head>
<body>
<button>Start</button>
<script src="find_time_slots.js" charset="utf-8"></script>
</body>
</hmtl>
find_time_slots.js
document.addEventListener('DOMContentLoaded', function(){
document.querySelector('button').addEventListener('click',onclick,false)
function onclick(res){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(
res.tabNum,
{code: 'document.getElementById("search").value = "overwatch";'}
);
})
}
},false)
content.js
chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
sendResponse({tabNum: sender.id})
});
虽然我没有收到任何错误,但它不会将搜索栏(在 youtube 上)更新为将“守望先锋”作为其值或显示在 youtube 页面中。
【问题讨论】:
标签: javascript google-chrome google-chrome-extension