window.open _self无法在background.js中工作 - chrome扩展

时间:2018-01-04 08:43:06

标签: google-chrome-extension window.open

早上好大师^^

首先,我只是有一个小问题,我无法解决o.o 我有一个谷歌Chrome扩展程序,使那些: - 如果鼠标事件发生,则打开另一个页面(本身detectclicks.js) - 如果直到5分钟没有发生鼠标事件,请打开前一页(本身为background.js)

我的问题是,当我们达到5分钟时,当我使用" _self"但是,如果我使用" _blank" ....但是在detectclicks.js中," _self"工作正常。也许在background.js下我无法使用" _self"参数Δ

我使用Google Chrome浏览器 - 版本63.0.3239.108(官方版本)(64位)(在Windows 10下)。

所以一切都工作得很完美(我的意思是计时器,并且还会重置计时器,而detectclicks.js正确打开网站本身..)就在background.js window.open" _self"我不想工作,只要我使用" _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();

感谢您支持^^,即使您刚刚阅读它:)

2 个答案:

答案 0 :(得分:0)

请使用此

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

而不是

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

更多参考请访问https://developer.chrome.com/extensions/tabs#method-create

答案 1 :(得分:0)

我可以用这种方式解决问题^^' 我知道这不是最好的解决方案,但它看起来像是我想要的。 (关闭网站并打开其他网站......看起来像更改活动网站^^')

    chrome.tabs.query({active:true,currentWindow:true},function(tabs){
         chrome.tabs.remove(tabs[0].id);
    });
    window.open("http://www.gmail.com","_blank");
    });

感谢您的帮助,当我以原始方式阅读您的解决方案时,但我不知道为什么在Chrome浏览器中打开新标签页面o.o ...

所以,即使我使用其他解决方案,我想我接受你的答案^^ 谢谢你的帮助:))