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

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

大师们早上好^^

首先,我有一个小问题我无法解决o.o 我有一个 google chrome 扩展程序,它使这些: - 如果发生鼠标事件,则打开另一页(本身 detectionclicks.js) - 如果直到 5 分钟没有发生鼠标事件打开前页(本身 background.js)

我的问题是,当我们到达 5 分钟时,当我使用“_self”时网站无法应用,但如果我使用“_blank”则应用...但是在 detectionclicks.js 中“_self”工作正常。也许在 background.js 下我不能使用“_self”参数?

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

所以一切都运行良好(我的意思是定时器,也重置定时器,detectionclicks.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();

感谢大家的支持^^,虽然只是看了:)

请使用这个

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

而不是

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

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

我可以这样解决问题^^' 我知道这不是最好的解决方案,但它看起来像我想要的。 (关闭站点并打开另一个站点...它看起来像更改活动站点^^')

    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 中打开网站的新标签...

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