简体   繁体   中英

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

I created an extension what is working in Firefox. I changed it for Google Chrome, but it is not working. 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. 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.

Do anybody have any idea, how can I resolve this problem?

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');
      });
    }

If you write in your managestartlapom.js file:

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

}

you will see that your window.location is equal to

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

and I think it is not the window.location you would like to replace. (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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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