2

Edit: Modified code using https://developer.chrome.com/extensions/devtools#evaluated-scripts-to-devtools as reference. Still no luck.

I'm trying to code a chrome-extension which uses chrome.* API call and save portions of the result in a file. I want to automate everything from the loading of the page to the text file download and hence, I don't want to use the browser.onclick() event. My current attempt has no effect.

What changes would I need to make?

https://stackoverflow.com/a/16720024

Using the above answer as reference, I attempted the following:

manifest.json

{
   "name":"Test Extension",
   "version":"0.0.1",
   "manifest_version": 2,
   "description":"Description",
   "permissions":["tabs"],
   "background": {
    "scripts": ["background.js"]
    },
   "devtools_page": "devtools.html"
}

background.js

// Background page -- background.js

chrome.runtime.onConnect.addListener(function(devToolsConnection) {
    // assign the listener function to a variable so we can remove it later
    var devToolsListener = function(message, sender, sendResponse) {
        // Inject a content script into the identified tab
        chrome.tabs.executeScript(message.tabId,
            { file: message.scriptToInject });
    }
    // add the listener
    devToolsConnection.onMessage.addListener(devToolsListener);

    devToolsConnection.onDisconnect.addListener(function() {
         devToolsConnection.onMessage.removeListener(devToolsListener);
    });
}

devtools.js

var backgroundPageConnection = chrome.runtime.connect({
    name: "devtools-page"
});

backgroundPageConnection.onMessage.addListener(function (message) {
    // Handle responses from the background page, if any
});

chrome.devtools.network.onRequestFinished.addListener(
    function(request) {
        chrome.runtime.sendMessage({
            string: "Hi",
            tabId: chrome.devtools.inspectedWindow.tabId,
            scriptToInject: "content.js"
        });

     }
);
chrome.runtime.sendMessage({
    string: "Hi",
    tabId: chrome.devtools.inspectedWindow.tabId,
    scriptToInject: "content.js"
});

content.js

alert("Hello");
  • *Please* don't load jQuery into **every** page unless you **need** it. jQuery is 85kiB of minimized code. This is a significant burden with which to saddle *every single page*. What of those of us with 100's of tabs open? While it's possible you really *need* jQuery, it's more likely that you are doing so for the convenience of saving a couple/few hundred characters in your own code by not using vanilla JavaScript. If that's the case, doing so is a *very* poor trade-off from your user's point of view. Your code currently avoids actually using jQuery, so it's unclear why you are loading it. – Makyen Aug 12 '17 at 20:37
  • There are lots of different things you can use to sense the current state of the page, both in your [content script and in your background script](https://stackoverflow.com/questions/2844565/is-there-a-javascript-jquery-dom-change-listener/39508954#39508954). Please [edit] the question to describe *exactly* the timing you are trying to accomplish and/or the reason you are waiting for `'completed'`, as it's not clear what you're attempting to accomplish by having the `tabs.onUpdated` listener wait for `'completed'` vs. the various other things you might be doing. – Makyen Aug 12 '17 at 20:49
  • What I'm trying to achieve is to save the response of all HTTP requests in a file. The idea was to use devtools.network to get this data and then send this data to content.js from where I could save it in a file. What would be the right way to achieve this? – Bishal Thingom Aug 13 '17 at 13:57

0 Answers0