Search code examples
google-chrome-extensionwindow.open

window.open _self not working in background.js - chrome extension


Good morning Masters ^^

Firstly, i just have a small problem what i cant solve o.o I have a google chrome extension program which made those : - If mouse event happening then open one other page (itself detectionclicks.js) - If until 5 minutes not happening mouse event open back the before page (itself background.js)

My problem is that, when we reach the 5 minutes, the site cant apper when i use "_self" but apper if i use "_blank".... but in detectionclicks.js the "_self" is working properly. Maybe under the background.js i cant use "_self" parameters?

I use Google Chrome browsers - Version 63.0.3239.108 (Official Build) (64-bit) (under windows 10).

So everything is working perfectly (I mean the timer, and reset the timer also, and the detectionclicks.js open the site itself properly..) just under the background.js window.open "_self" wont want working, just if i use "_blank".

Manifest.json

    {
         "name": "Chrome-extension",
         "version": "1.0",
         "manifest_version": 2,

         "permissions": [
              "alarms", "cookies"   
         ],
   "background": {
        "scripts": ["background.js"]
        },
   "content_scripts": [
        {
             "matches": ["<all_urls>"],
             "js": ["jquery1.7.2.js", "detectclicks.js", "background.js"]
        }
   ],
   "description": "Chrome-extension",
   "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
        } 
   }

Background.js

        chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) {
         if (msg.setAlarm) {
              chrome.alarms.create({delayInMinutes: 0.1});
         } else if (msg.delayedResponse) {
              setTimeout(function() {
                   sendResponse("Got your message.");
              }, 300000);
         return true;
         } else if (msg.getCounters) {
              sendResponse({counter: counter++,
              persistentCounter: localStorage.counter++});
         }   
    });
    chrome.alarms.onAlarm.addListener(function() {
         window.open("http://www.google.com","_blank"); //THIS IS WORK
         // window.open("http://www.google.com","_self"); THIS ISN'T WORK
    });

Detectionclicks.js

    $('html').on('click', '*', function(event) {
         window.open("http://gmail.com","_self");
    });
    $('html').on('mousemove','*', function(event) {
         chrome.runtime.sendMessage({setAlarm: true});
    });

Popup.html

    <style>
         div.content { width: 250px }
    </style>
    <!DOCTYPE html>
    <html>
         <body>
              <div class="content">
                   <p>
                   Site information : This extension will open a new site
                   after 5 minutes if wont happening a mouse event. If mouse
                   event happening, the extension will open an other site.
                   </p>
              </div>
         </body>
    </html>

Popup.js

    chrome.extension.getBackgroundPage().timers[tabId] = new Date();

Thank you for your supporting ^^, even if you just read it :)


Solution

  • Please use this

    chrome.tabs.create({ url: "http://gmail.com" });
    

    instead of

    window.open("http://gmail.com","_self");
    

    more reference please visit https://developer.chrome.com/extensions/tabs#method-create