我试图开发一个谷歌铬扩展,可以阻止特定的URL。在google的帮助下,我编写了以下代码,但代码并不完美。
守则:
manifest.json
{
"manifest_version": 2,
"name": "Zitu-LinkBlocker",
"description": "A link blocker extension!",
"permissions": [
"tabs"
],
"version": "1.0",
"icons": {
"128": "icon128.png",
"48": "icon48.png",
"16": "icon16.png"
},
"browser_action": {
"default_icon": "icon16.png",
"default_popup": "popup.html"
}
}popup.js
$(document).ready(function() {
chrome.tabs.query(null, function (tab) {
var link = document.createElement('a');
link.href = tab.url;
$('#host').html("Host : "+link.hostname);
})
}); popup.html
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
body {
min-width: 357px;
overflow-x: hidden;
}
</style>
<script src="jquery-3.5.1.min.js"></script>
<script src="popup.js"></script>
</head>
<body>
Yo Man
<div id="host"></div>
</body>
</html> 发布于 2020-07-31 04:33:24
我认为您需要在{ active: true, currentWindow: true }方法中传递chrome.tabs.query而不是null。回调中的选项卡参数将是一个数组,您需要使用tab.url访问该URL。
manifest.json
{
"manifest_version": 2,
"name": "Zitu-LinkBlocker",
"description": "A link blocker extension!",
"permissions": [
"tabs"
],
"version": "1.0",
"browser_action": {
"default_popup": "popup.html"
}
}popup.html
<!doctype html>
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
body {
min-width: 357px;
overflow-x: hidden;
}
</style>
<script src="jquery.min.js"></script>
<script src="popup.js"></script>
</head>
<body>
Yo Man
<div id="host"></div>
</body>
</html>popup.js
$(document).ready(function () {
chrome.tabs.query({ active: true, currentWindow: true }, (tab) => {
console.log(tab)
var link = document.createElement('a');
link.href = tab[0].url;
$('#host').html("Host : " + link.hostname);
});
}); https://stackoverflow.com/questions/63177770
复制相似问题