0

I have been following tutorials but NONE of them will open a link using the extension! I tried this code and it won't do anything but show a loading sign and go away. manifest.json:

{
"name": "popphelp Helpful Links",
"version": "0.1",
"description": "Get to helpful links faster!",
"manifest_version": 2,
 "permissions": [ "tabs", "storage", "http://*/*", "https://*/*" ],
  "background": {
      "scripts": [ "jquery-1.11.3.min.js", "background.js" ],
        "persistent": false
  },
  "browser_action": {
  "defualt_icon": "icon.png",
"default_popup": "mypopup.html"
  }
}

background.js:

// background.js

// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tabs) {
  // Send a message to the active tab
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var activeTab = tabs[0];
    chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
  });
});

// This block is new!
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "open_new_tab" ) {
      chrome.tabs.create({"url": request.url});
    }
  }
);

content.js:

// content.js
chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "clicked_browser_action" ) {
      var firstHref = $("a[href^='http']").eq(0).attr("href");

      console.log(firstHref);

      // This line is new!
      chrome.runtime.sendMessage({"message": "open_new_tab", "www.google.com": firstHref});
    }
  }
);

jquery:

<script
  src="http://code.jquery.com/ui/1.11.43/jquery-ui.js"
  integrity="sha256-DI6NdAhhFRnO2k51mumYeDShet3I8AKCQf/tf7ARNhI="
  crossorigin="anonymous"></script>
Denis L
  • 3,209
  • 1
  • 25
  • 37
  • What *exactly* is shown in the [various appropriate consoles for your extension](http://stackoverflow.com/a/38920982/3773011) when you load and execute your extension? – Makyen Aug 10 '17 at 00:28
  • I suggest you read the [Chrome extension overview](https://developer.chrome.com/extensions/overview) (perhaps along with the pages linked from the overview). The [architecture section](https://developer.chrome.com/extensions/overview#arch) has overall architecture information which should help your understanding of how things are generally organized/done. You will probably also want to read [Content Scripts](https://developer.chrome.com/extensions/content_scripts), and [Message Passing](https://developer.chrome.com/extensions/messaging). – Makyen Aug 10 '17 at 00:28
  • Your code has multiple issues, some of which should be indicated by errors in the various consoles. Primarily, you appear to be attempting to communicate between your background script and a content script. However, you have nothing that will actually inject that content script (e.g. a call to [`chrome.tabs.executeScript`](https://developer.chrome.com/extensions/tabs#method-executeScript) or a `content_scripts` entry in your *manifest.json*). Some other issues: you load jQuery in youre background scripts, this is *rarely* a good idea. In your *content.js* `"www.google.com"` should be `url`. – Makyen Aug 10 '17 at 00:40
  • You use `tabs.query` to get the Id for the current tab in the active window when you already have that information in the [`tab`](https://developer.chrome.com/extensions/tabs#type-Tab) (not `tabs`) that is passed to your [`browserAction.onClicked` listener](https://developer.chrome.com/extensions/browserAction#event-onClicked). – Makyen Aug 10 '17 at 00:42

0 Answers0