简体   繁体   English

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

[英]How can I replace the popup window with an other page in chrome extension?

I created an extension what is working in Firefox. 我创建了一个在Firefox中正常工作的扩展程序。 I changed it for Google Chrome, but it is not working. 我已将其更改为Google Chrome浏览器,但无法正常工作。 When I click on the extension's icon then popup shown with two menu option. 当我单击扩展程序的图标时,将显示带有两个菜单选项的弹出窗口。 But when I click on any of these links nothing happened. 但是,当我单击任何这些链接时,都没有发生。 If I press right click on any of these links and choose open in new tab then this html file shown in a new tab. 如果我按右键单击任何这些链接,然后选择“在新选项卡中打开”,则此html文件将在新选项卡中显示。 If I click on links in this new tab php file will be open and works, so I think window.location.replace does not work in crome extension. 如果我单击此新选项卡中的链接,则php文件将打开并起作用,因此我认为window.location.replace在crome扩展中不起作用。

Do anybody have any idea, how can I resolve this problem? 有人有什么想法,我该如何解决这个问题?

manifest.json 的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 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 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');
      });
    }

If you write in your managestartlapom.js file: 如果您写在managestartlapom.js文件中:

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

} }

you will see that your window.location is equal to 您将看到window.location等于

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

and I think it is not the window.location you would like to replace. 而且我认为这不是您要替换的window.location (it is the window of your popup menu). (这是您的弹出菜单的窗口)。 If you want to replace window.location of your active tab page, you should use chrome.tabs.executeScript in your chrome.tabs.query callback. 如果要替换活动标签页的window.location ,则应在chrome.tabs.query回调中使用chrome.tabs.executeScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM