0

I am working on simple Chrome Extension with the aim of opening every link on a page with the class of entry. Currently, I have this....

manifest.json:

{
    "manifest_version": 2,

    "name": "Hello World",
    "description": "A simple Chrome Extension",
    "version": "1.0",

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

    "permissions": [
        "tabs",
        "http://*/",
        "https://*/"
    ],
    "browser_action": {
        "default_icon": "logo.png"
    }
}

openlinks.js:

chrome.browserAction.onClicked.addListener(function(tab) {
    var linkArray = ['https://www.google.com', 'http://www.bbc.co.uk']; // your links
    for (var i = 0; i < linkArray.length; i++) {

    chrome.tabs.create({
        url: linkArray[i]
    });
}
});

Now I am trying to replace the array of sample links with an array of links from the current tab. Is it just a case of using standard JavaScript or jQuery to achieve this?

Makyen
  • 31,849
  • 12
  • 86
  • 121
fightstarr20
  • 11,682
  • 40
  • 154
  • 278
  • Implementing this would be fairly involved for a Stack Overflow answer -- injecting a content script, producing the array of links, messaging the extension, and creating the tabs in response to the message. Can you narrow down your question? – Josh Lee Jan 11 '17 at 20:51
  • Had not realised it would be so involved! Am I not already injecting the content script with openlinks.js? – fightstarr20 Jan 11 '17 at 20:52

1 Answers1

1

Take a look at Chrome Extensions Overview # Architecture, because you'll need both an Event Page and a Content Script to make this happen.

Here's an outline of how I would go about solving this:

  1. Manifest structure (Event Page + activeTab permission).

    "background": { "scripts": ["bg.js"], "persistent": false },
    "permissions": ["activeTab"],
    "browser_action": {},
    
  2. When the browser action is clicked, the browser grants permission to access the current tab, which we use to inject the script. See Content Scripts # Programmatic Injection.

    // bg.js
    chrome.browserAction.onClicked.addListener(tab =>
      chrome.tabs.executeScript({file: 'content.js'});
    });
    
  3. The content script has permission to access the DOM and use message passing, but is restricted from most of the extension APIs (in particular chrome.tabs).

    // content.js
    message = {}
    message.links = [...document.querySelectorAll(
            'div.question-summary a.question-hyperlink')].map(e=>e.href);
    chrome.runtime.sendMessage(message);
    
  4. The background page listens for the message.

    // bg.js
    chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
      request.links.forEach(link => chrome.tabs.create({url: link});
    });
    
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • Given that the content script is completing execution, it would be more efficient to return the data as the results of the content script and receive it in the `tabs.executeScript()` callback (given that you should have a callback to handle error conditions (e.g. the user clicking the browser action button on pages into which you are not permitted to inject scripts)). If you do it that way there is no need for the additional complication of using [Message Passing](https://developer.chrome.com/extensions/messaging). – Makyen Jan 11 '17 at 21:17
  • Thank you Josh! Reading through it now – fightstarr20 Jan 11 '17 at 21:18
  • 1
    Saying content scripts have "no other extension permissions" is inaccurate. While there is no need to list the available APIs, something like the following would still be brief, but more accurate: "For a complete list of the very limited `chrome.*` extension APIs that are permitted to content scripts, see the [Content Script documentation](https://developer.chrome.com/extensions/content_scripts)." – Makyen Jan 11 '17 at 21:26
  • @Makyen How does that work? What does "result of the script" mean? – Josh Lee Jan 11 '17 at 21:49
  • @JoshLee: Ironically, I [answered a question about that yesterday](http://stackoverflow.com/questions/41577988/chrome-tabs-executescript-how-to-get-result-of-content-script). I agree that the wording of the documentation is poor. In addition to the Chrome documentation, the [Firefox WebExtension `tabs.executeScript()` documentation](https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript) also suffered from the same wording, so I updated it yesterday to be more specific and have a brief example. – Makyen Jan 11 '17 at 22:43