簡體   English   中英

如何在Google Chrome擴展程序中檢測我是否處於隱身模式?

[英]how to detect whether i am in incognito mode in google chrome extension application?

function test(){
alert(chrome.extension.inIncognitoContext);
if(chrome.extension.inIncognitoContext){
   chrome.tabs.query({currentWindow: true, active: true},function(tabs){
      alert(tabs[0].url);
    });
}
}

document.addEventListener('DOMContentLoaded', function() {
    var link = document.getElementById('link');
   // onClick's logic below:
    link.addEventListener('click', function() {
    test();
  });
});

這是我的chrome擴展程序應用程序的.js文件,在該應用程序中,我在單擊擴展程序中的按鈕時調用test()函數(以下是擴展程序的html代碼)。 在隱身模式和默認Chrome瀏覽器中,它都警告為false。 我在另一個參考資料中看到使用boolean變量chrome.extension.inIncognitoContext進行檢查 我錯了,代碼有什么問題嗎?

<!doctype html>
<!--
This page is shown when the extension button is clicked, because the
"browser_action" field in manifest.json contains the "default_popup" key with
value "popup.html".
-->
<html>
<head>
<title>Getting Started Extension's Popup</title>
<style>
  body {
    font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
    font-size: 100%;
  }
  #status {
    /* avoid an excessively wide status text */
    white-space: pre;
    text-overflow: ellipsis;
    overflow: hidden;
    max-width: 400px;
  }
</style>

<!--
  - JavaScript and HTML must be in separate files: see our Content Security
  - Policy documentation[1] for details and explanation.
  -
  - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
 -->
<script src="popup.js"></script>
</head>
<body>
<button id= "link" onclick="test()">store the page</button>
</body>
</html>

isIncognitoContextChrome文檔對此屬性的說明如下:

對於在隱身標簽頁中運行的內容腳本以及在隱身進程中運行的擴展頁面為True。 后者僅適用於具有'split'incognito_behavior的擴展名。

您的代碼在彈出窗口中,這意味着它是擴展頁面。 它不是內容腳本。 根據上面的描述,如果清單具有頂級屬性"incognito": "split" ,則擴展頁面僅會看到isIncognitoContexttrue值。

您可能"spanning"或根本沒有。 根據此頁面上的隱身清單選項 ,默認情況下為"spanning"

擴展程序和Chrome應用程序的默認設置為"spanning" ,這意味着它將在單個共享進程中運行...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM