有没有办法让 chrome 扩展程序始终监听?
Is there a way for a chrome extension to always listen?
我构建了一个 chrome 扩展程序,通过获取他们的 URL 来跟踪您访问过的站点。问题是,当扩展名是 opened/clicked 时,它只接收页面的 URL。我怎样才能使它始终“收听”我访问的网站?
我试图通过使其在站点加载时获得 URL 来修复它,但我认为发生的事情是它在 EXTENSION 加载时进行监听:
window.onload = function () {
chrome.tabs.getSelected(null, function (tab) {
d = document;
f = document.getElementById("history");
var i = d.createElement('li');
i.innerHTML = tab.url;
f.appendChild(i);
d.body.appendChild(f);
localStorage.setItem("pastVisits", f.innerHTML);
});
try { // trycatch because what if there's no history to pull?
document.getElementById("history").innerHTML += localStorage.getItem("pastVisits");
} catch (error) {
// do nothing...
}
}
body {
width: 800px;
height: 600px;
background-color: rgb(155, 155, 155);
color: white;
font-family: 'Courier New', Courier, monospace;
}
<!DOCTYPE html>
<html>
<head>
<title>User Locations</title>
<script src="popup.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p id="history">
</p>
</body>
</html>
添加了代码片段,尽管它出于某种原因在代码片段中不起作用(尽管它在扩展中起作用)。这是它的图片:
您将添加 "history" permission。
// manifest.json
{
"name": "My extension",
...
"permissions": [
"history"
],
...
}
然后,使用
// background.js
chrome.history.onVisited.addListener(
result => console.log(result)
)
“在访问 URL 时触发,为 URL 提供 HistoryItem 数据。此事件在页面加载之前触发。”
我构建了一个 chrome 扩展程序,通过获取他们的 URL 来跟踪您访问过的站点。问题是,当扩展名是 opened/clicked 时,它只接收页面的 URL。我怎样才能使它始终“收听”我访问的网站?
我试图通过使其在站点加载时获得 URL 来修复它,但我认为发生的事情是它在 EXTENSION 加载时进行监听:
window.onload = function () {
chrome.tabs.getSelected(null, function (tab) {
d = document;
f = document.getElementById("history");
var i = d.createElement('li');
i.innerHTML = tab.url;
f.appendChild(i);
d.body.appendChild(f);
localStorage.setItem("pastVisits", f.innerHTML);
});
try { // trycatch because what if there's no history to pull?
document.getElementById("history").innerHTML += localStorage.getItem("pastVisits");
} catch (error) {
// do nothing...
}
}
body {
width: 800px;
height: 600px;
background-color: rgb(155, 155, 155);
color: white;
font-family: 'Courier New', Courier, monospace;
}
<!DOCTYPE html>
<html>
<head>
<title>User Locations</title>
<script src="popup.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p id="history">
</p>
</body>
</html>
添加了代码片段,尽管它出于某种原因在代码片段中不起作用(尽管它在扩展中起作用)。这是它的图片:
您将添加 "history" permission。
// manifest.json
{
"name": "My extension",
...
"permissions": [
"history"
],
...
}
然后,使用
// background.js
chrome.history.onVisited.addListener(
result => console.log(result)
)
“在访问 URL 时触发,为 URL 提供 HistoryItem 数据。此事件在页面加载之前触发。”