简体   繁体   English

更新Chrome扩展程序:清单2和安全策略

[英]update a chrome extension: manifest 2 and security policy

I am trying to update an existing chrome extension that used background page, I found something here and here I get no error but the popup is never displayed. 我正在尝试更新使用背景页面的现有chrome扩展程序,我在这里找到了一些东西, 这里我没有收到任何错误,但是从不显示弹出窗口。 I even tried to go back to some old chrome version to allow me trying the manifest 1 code but the time doesn't increase. 我什至尝试回到一些旧的chrome版本,以允许我尝试使用manifest 1代码,但时间没有增加。 I am sorry for all this code but I have no idea where the problem is coming from. 对于所有这些代码,我感到很抱歉,但是我不知道问题出在哪里。

Manifest.json Manifest.json

    {

"manifest_version": 2,
    "background": {
    "scripts": ["background.js"]
  },

   "browser_action": {
      "default_icon": "icon.png",
      "popup": "popup.html"
   },
   "description": "Count the time on Facebook",
   "name": "Compteur Facebook ",
   "permissions": [ "tabs" ],
   "version": "1.0"
}

popup.html popup.html

 <!doctype html>
    <html>
      <head>
      <script src="popup.js"></script>
      </head>
      <body>
     The amount of time you have spent on <span>Facebook</span> is: 
    <br /><br />
    <script>document.write(prettyDate(localStorage.getItem('facebookCounter')));</script>
    <br /><br />

    <div>
    Having a problem?<br />

    <button>Reset</button>
    </div>
    </body>
    </html>

popup.js popup.js

chrome.extension.getBackgroundPage();

    function resetCounter(){ localStorage.setItem('facebookCounter',0)}

function prettyDate(time){            
        var responce= "About " + Math.round(time) + " seconds"
        return responce
}
function main() {}

document.addEventListener('DOMContentLoaded', function () {
  document.querySelector('button').addEventListener('click', clickHandler);
  resetCounter();
  main();
});

background.js background.js

var a = 0
  var x
  function timedCount()
  {
    a = parseInt(localStorage.getItem('facebookCounter'))

    chrome.tabs.getSelected(null, function(tab) 
    {

        theurl = tab.url.substr(0,24)

        if(theurl == "http://www.facebook.com/")
        {
            a=a+1 
            localStorage.setItem('facebookCounter',a)
        }
    });

    setTimeout("timedCount()",1000);  

  } 

  setTimeout("timedCount()",1000); 

For the popup to appear, you have to modify your "manifest.json", replacing 为了显示弹出窗口,您必须修改“ manifest.json”,替换为
"popup": "popup.html"
with
"default_popup": "popup.html"

You can find more info about the transition from Manifest v1 to Manifest v2 here . 您可以在此处找到有关从Manifest v1到Manifest v2过渡的更多信息。

Also, note that you probably need to make several changes to make your extension more efficient (eg turning your background page into an event page, or capturing some events instead of using a timer fired every second). 另外,请注意,您可能需要进行一些更改以使扩展更有效(例如,将背景页面转换为事件页面,或者捕获某些事件,而不是使用每秒触发的计时器)。 This transition guide might be a good place to start. 本过渡指南可能是一个不错的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Chrome 扩展 Javascript content_security_policy 清单 3 - Firebase Chrome Extension Javascript content_security_policy Manifest 3 Chrome 扩展清单 v3 内容安全政策 - Chrome extension manifest v3 Content Security Policy 如何在 Chrome Extension Manifest.json 中设置内容安全策略以使 Firebase 工作 - How to set Content Security Policy in Chrome Extension Manifest.json in order for Firebase to work Chrome 扩展程序:内容安全策略错误 - Chrome Extension: Content Security Policy Error Extjs,Chrome扩展程序和内容安全政策 - Extjs, Chrome Extension and Content Security Policy Chrome扩展程序内容安全政策和权限 - Chrome extension content security policy and permissions chrome扩展名的内容安全政策问题 - content security policy issue with chrome extension Chrome 扩展提取 API - 内容安全政策 - Chrome extension fetch API - Content Security Policy Chrome扩展程序内容安全策略指令错误 - Chrome extension Content Security Policy directive error Gmail Chrome 扩展“拒绝加载脚本,因为它违反了以下内容安全策略指令:“script-src 'self'”。Manifest v3 - Gmail Chrome Extension "Refused to load script because it violates the following Content Security Policy directive: "script-src 'self'". Manifest v3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM