Chrome extension to redirect tab to my page

后端 未结 3 951
醉话见心
醉话见心 2021-01-01 06:40

I am new to chrome extensions and I am having trouble while writing my first extension for chrome. What I am trying to do is detect the new tab action and redirect to a pre-

相关标签:
3条回答
  • 2021-01-01 07:09

    With the new chrome extensions you should not put any script code in the background page.

    Inline scripts and event handlers disallowed

    Due to the use of Content Security Policy, you can no longer use tags that are inline with the HTML content. These must be moved to external JS files. In addition, inline event handlers are also not supported. For example, suppose you had the following code in your extension:

    https://developer.chrome.com/extensions/tut_migration_to_manifest_v2.html#inline_scripts

    What you should do is move your code to a js file, either set the manifest background property to a js file or have a background page that links to an external js file and put all you js code there...

    Try using the background property: http://developer.chrome.com/extensions/background_pages.html

    1)basically add to your manifest a background script (you do not need the background page for your extension right now)

    background : {
    "scripts": ["background.js"]
    }
    

    2) Then add a a background.js to your extension directory and put all your js code there..

    Then try again, and see if the extension works :)

    3) remove all html from the js file and put just you js in the background.js

    chrome.browserAction.onClicked.addListener(function(tab){
                var action_url = "www.xyz.com"
                chrome.tabs.create({ url: action_url });
        }
    
    0 讨论(0)
  • 2021-01-01 07:23

    Add this guy to your manifest.json file.

    "chrome_url_overrides": {
          "newtab": "index.html"
       }
    

    with index.html being replaced with your page. This will make it so it will show index.html when you open a new tab instead of the default.

    I have an extension that basically does the same thing. Here's my manifest.json that you can use as a reference.

    {
      "name": "appName",
      "version": "0.1",
      "manifest_version": 2,
      "chrome_url_overrides": {
          "newtab": "index.html"
       },
    
      "description": "Desc Here",
      "icons": {
        // "128": "icon128.png"
      },
      "content_scripts": [ {
      "css": ["css/styles.css"],
      "js": [ ],
      "matches": [ "*://*/*" ],
      "run_at": "document_start"
      } ],
      "minimum_chrome_version": "18",
      "permissions": [ "http://*/*", "https://*/*", "cookies", "tabs", "notifications" ]
    
    }
    
    0 讨论(0)
  • 2021-01-01 07:29

    If you want to over ride a default page, you shoud use html file, not a js file. And if you just want to over ride a page, you do not need any background page or content script. Here is the sample code:

    menifest.json:

    {
        "name": "Tabber",
        "manifest_version" : 2,
        "version": "1.0",
        "description": "MyExtension",
        "chrome_url_overrides": {
            "newtab": "my.html"
        },
        "permissions": [
            "tabs"
        ]
    }
    

    my.html:

    <!DOCTYPE html>
    <html>
    <head>
        <title>over ride page</title>   
        <script type="text/javascript" src="code.js">
        </script>
    </head>
    <body>    
    </body>
    </html>
    

    code.js:

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

    Note: you can not write any js in your html file. You need to include js file in html file.

    And, you even do not need any js file. just modify the my.html as this:

    <head>
        <meta http-equiv="refresh"content="0;URL=http://www.google.com">
    </head>
    
    0 讨论(0)
提交回复
热议问题