如何用chrome扩展中的其他页面替换弹出窗口



我创建了一个在Firefox中工作的扩展。我把它改成了谷歌浏览器,但它不起作用。当我点击扩展的图标,然后弹出显示两个菜单选项。但当我点击这些链接时,什么也没发生。如果我右键点击这些链接中的任何一个,并选择在新选项卡中打开,那么这个html文件就会显示在新选项卡上。如果我点击这个新选项卡中的链接,php文件就会打开并工作,所以我认为window.location.replacement在chrome扩展中不工作。

有人知道我该如何解决这个问题吗?

manifest.json

{
"browser_action": {
"default_icon": {
"48": "images/startlapom-48.png",
"96": "images/startlapom-96.png"
},
"browser_style": true,
"default_title": "Startlapom",
"default_popup": "managestartlapom.html"
},
"description": "Oldal hozzáadása, levétele a Startlapom oldalam(ra/ról)",
"manifest_version": 2,
"name": "Startlapom",
"permissions": ["tabs"],
"version": "1.0"
}

managestartlapom.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Startlapom</title>
<link rel="stylesheet" href="managestartlapom.css"/>
<link rel="stylesheet" href="https://startlapom.eu/startlapom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body class="addon-body">
<div id="startlapom-addon">
<div class="panel">
<a class="addon-link" href="#" id="startlapom-add">Oldal hozzáadása a Startlapomhoz</a><br />
<div class="panel-section-separator"></div>
<a class="addon-link" href="#" id="startlapom-remove">Oldal levétele a Startlapomról</a><br />
</div>
</div>
<script src="managestartlapom.js"></script>
</body>
</html>

managestartlapom.js

document.getElementById('startlapom-add').onclick = function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
window.location.replace('https://startlapom.eu/addon.php?url='+encodeURIComponent(tabs[0].url)+'&title='+encodeURIComponent(tabs[0].title)+'&reason=ADD');
});
}
document.getElementById('startlapom-remove').onclick = function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
window.location.replace('https://startlapom.eu/addon.php?url='+encodeURIComponent(tabs[0].url)+'&title='+encodeURIComponent(tabs[0].title)+'&reason=REM');
});
}

如果您在managestartlapom.js文件中写入:

....
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
alert(window.location);
....... 

}

你会看到你的window.location等于

chrome-extension://ext_id_here/managestartlapom.html#

我认为你想更换的不是window.location。(这是弹出菜单的窗口(。如果要替换活动选项卡页面的window.location,则应在chrome.tabs.query回调中使用chrome.tabs.executeScript。

最新更新