redirect to page.html after chrome extension installs

可紊 提交于 2019-12-21 21:33:40

问题


Here is my code, but it refuses to complile on the chrome developer dashboard. I have a page called my-page.php on the server

 {
   "content_scripts": [ {
      "all_frames": true,
      "js": [ "go.js" ],
      "matches": [ "\u003Call_urls\u003E" ]
   } ],
   "description": "blahhh",
   "icons": {
      "128": "icon128.png",
      "16": "icon16.png",
      "48": "icon48.png"
   },
   "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCdw+eIzaqEWfjrzJZ1TFO0/QVxWNdQEMvf4V6xdpZpTfIW3lOPVIJIvA6D4wyv6H+C8KAgbh58JEkqzgEC/1a+r4jGXhbvQk7Ybjj2CMVJBe6jF5Fz0ckPyTlPreFkT13PGwi72lljRZz7680VwY9jjPa7rsjV4hjTt2RRfi3UfwIDAQAB",
   "name": "my plugin",
      "background_page": "mypage.php",
      "permissions": [ "tabs", "*://*/*" ],
   "version": "1.3.0"
}

回答1:


Chrome now has an onInstalled event. Most of you reading this probably know this already, but for those who (like me) search SO instead of reading the actual docs, check out:

https://developer.chrome.com/extensions/runtime.html#event-onInstalled




回答2:


Your background page is to be packaged with the rest of your files. You can't have a background file that is on your server. It's throwing an error because you probably don't have a file called mypage.php in the folder for your extension. Learn about background pages.

There are no events that let your extension know when it has been installed, but a simple way to do it, would be to add code like this in your background file:

if(!localStorage.first){
    chrome.tabs.create({
       url : "http://whatever.com/welcome.html"
    });
    localStorage.first = "true";
}

This would work because the background file's code would be executed right after it was installed, and if localStorage.first didn't exist already, it would open the tab and set localStorage.first to true, so that it didn't open the tab the next time the background file's code was executed, i.e. when the browser was reopened.

Note that the tab will open if the user clears all localStorage as well. This was the only way I could think of, though.



来源:https://stackoverflow.com/questions/8553042/redirect-to-page-html-after-chrome-extension-installs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!