0

I have a unstable internet connection and would like to monitor the router log via a chrome extension. Since i am just starting with programming i decided to create a new tab browserAction calling a html page that simply contained a HTTP-EQUIV="REFRESH" pointing at my browser log. That way i wiggled out of the same origin policy. That since stopped working without a reason or error log entry i could start from. Could anybody give me some pointers as to how i could get this back working, improve this and or tell me how weird/wrong my approach is? :)

manifest.json:

{
    "name": "Logview",
    "version": "0.1",
    "manifest_version": 2,
    "description": "Logview Extension",
    "browser_action": {
      "default_icon": "icon.png",
      "default_title": "Logview Extension"
    },
    "background": {
      "page": "background.html"
    },
    "permissions": [
      "tabs"
    ]
} 

background.html

<html>
    <head>
        <script>
        chrome.browserAction.onClicked.addListener(function(tab) {
            chrome.tabs.create({url: "redirect.html"});
        });
        </script>
    </head>
</html>

redirect.html

<head>
    <meta HTTP-EQUIV="REFRESH" content="0; url=http://user:pass@192.168.1.1/logview.cmd">
</head>

Please be gentle, its my first post here and i researched a ton of stuff before posting. The meta refresh was the only thing i could come up with that went around same origin hazards or public apis that i cannot use in the event of the router being down. All obvious errors were made out of inexperience rather than negligence. I just hit a wall with it just not doing anything anymore.

All the best, morph

edit: changed the title to ...new tab (was ...pop-up)

penkzhou
  • 1,200
  • 13
  • 30

1 Answers1

0

You'r bitten by the beast called Content Security Policy. Don't worry, you are not the first one to experience this, I see this mistake getting repeated every week.

Before jumping to the solution, I want to equip one with the tools to solve the issue, by presenting two previous answers:

After reading the last answer, you should understand the problem with your code, and understand that the solution looks like:

background.html

<script src="background.js"></script>

background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.create({url: "redirect.html"});
});

If your background page consists of only scripts, just use the background.scripts syntax in the manifest file. Then, you don't need background.html any more.

manifest.json (part of it)

"background": {
   "scripts": ["background.js"],
   "persistent": false
},

Final notes:

  • I have added "persistent":false to your manifest file, to turn your background page into an event page. Most of the time, your extension is doing nothing, so using an event page instead of a background page saves memory.
  • You can safely remove the tabs permission from the manifest file. It is not needed if you only want to open a new tab.
Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thank you very much for that extensive answer. You have an instant fan, klicking on your username opened a whole treasure trove for me. It appears the worst wrong assumptions you have are the ones you don't know you have. No different were your final remarks. I was under the impression that "persistent": false was implied if i don't set it to true - as well as i read the documentation of for tabs wrong: "Use the chrome.tabs module to interact with the browser's tab system. You can use this module to create, modify, and rearrange tabs in the browser." –  Jul 13 '13 at 23:49
  • Now since that works so well, if you don't mind me asking: In the above scenario. Where would i have to look for doing things like parsing the generated router log page for say a certain entry? That probably has been done before often, but i'm not sure where to start. Most Questions are about sites that are already open in a tab or similar. Uhm and i just realize, is it good practise to ask that in a new question? –  Jul 14 '13 at 08:45
  • @morph Yes, for entirely different questions, you should create a new question, because it generates more exposure. And who knows, maybe it's useful to some future visitor with the same problem ;) To answer your question: Use [content scripts](https://developer.chrome.com/extensions/content_scripts.html) to read data from the page. – Rob W Jul 14 '13 at 08:47