Question

I want to make a Chrome Extension that opens every second a list of URL's. I don't have much experience with making Chrome Extensions/Apps. I already have the biggest part of the code but something doesn't work well. I tried many things that i find on the internet. I tried to use an Chrome App instead of a Chrome Extension but that will show some errors. If i start the code it doesn't show errors in the console but nothing happenend. I hope you will find a solution!

The code.

manifest.js

{
    "name": "PageViewer",
    "description": "Page Viewer extensions.",
    "browser_action": {
        "default_icon": {
            "38": "logo-16.png"
        },
        "default_title": "Ready for start!",
        "default_popup": "window.html"
    },
    "icons": {
        "128": "logo-128.png",
        "16": "logo-16.png"
    },
    "permissions": [
        "tabs"
    ],
    "manifest_version": 2,
    "version": "0.1"
}

window.html

<!DOCTYPE html>
<html>
    <head>
        <script src="background.js"></script>
    </head>
    <body>
        <div><input type="button" value="Start" class="start"></div>
    </body>
</html>

background.js

chrome.app.runtime.sendRequest.addListener(function() {
    var param = 0;

    var reload = {
        page: function() {
            chrome.tabs.getCurrent(function (tab) {
                param++;
                chrome.tabs.update(tab.id, {url: "http://testsite.com/id/" + param});
            });
        }
    }

    document.getElementsByClassName("start").onclick = function() {
        setInterval(reload.page(), 11000);
    }

});
Était-ce utile?

La solution

First, your background.js will not be running in the background the entire time the browser is opened. It is triggered by the opening of the popup. Useful explaination. If you want it to act in the background, remove it from your .html and add the following to your manifest:

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

Now on the chrome://extensions page, you should see "Inspect views: background page" by your app. However, you may not even need a background script for your extension. If you only want it to act when the icon is clicked (the browser action), then you won't need to add this.

Second, as mentioned, the .app.* of your listener signified it is only for apps, not extensions.

For some reason I was having trouble with your onclick event and I'm not that familiar with it. In the script what I usually do is

function fillPopup(){
  var elem = document.getElementById('start');
  elem.addEventListener('click', funcdostuff);
}
document.addEventListener('DOMContentLoaded', fillPopup)

I'm not fully sure what you are trying to initialize with the click of the start button. My advice would be to draw out or outline the path of initialization for what you are trying to do and then make sure that is valid with the api's. For example, your initial code would be

Waits for a browser action of clicking the icon (defined in manifest)
Which launches the .html
The .html then launches the script.
The script waits for a the start button to be pressed

I apologize if this advice isn't that relevant for you, but since you said you are new to chrome apps and plugins, I personally find it a good place to start when deciding what to put where.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top