0

I know I must be missing something obvious but I've spent the past 2 hours trying to work this one out. What I'm trying to do is get some information from the active tab, specifically a variable from the URL and the content of a textarea.

I can't seem to retrieve either of these from within my Chrome extension

Here is my ticket.js

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

chrome.tabs.query({currentWindow: true, active: true}, function(tabs){
 console.log(tabs[0].url);
 var ticketID = getQueryVariable("id");
 console.log(ticketID);
 var inputTicket= document.getElementById('bottom_message').value;
 console.log(inputTicket);
});

Now when I run it nothing happens. My popup.js is blank and I currently don't need it to do anything popup related, however, do I need to reference ticket.js within popup.js?

Here is my manifest

{
  "manifest_version": 2,

  "name": "Autosave",
  "description": "Extension Ticket system to autosave text in a ticket",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
  },
  "permissions": [
    "activeTab",
 "tabs"]
 
  "content_scripts": [
   "matches": ["https://portal.ticketsystem.co.uk/*"],
   "js": ["ticket.js"]
}
jblz
  • 1,001
  • 2
  • 13
  • 33
  • It could be, however, my popup.js is called as a content_script so shouldn't it be accessing the page? I've renamed my popup.js to ticket.js and updated the manifest, then created a blank popup.js. Now it does nothing. Do I have to reference ticket.js within popup.js? I've updated the question to reflect this – jblz Sep 19 '15 at 23:34
  • 1
    `chrome.tabs` is not available to a content script. And you don't need it in this case, either. You can get the current url from `location.href` – rsanchez Sep 19 '15 at 23:42

1 Answers1

0

Your manifest has errors. Doesn't Chrome indicate this when loading the extension? Compare with yours and note that I added a few ,[]{}:

{
  "manifest_version": 2,

  "name": "Autosave",
  "description": "Extension Ticket system to autosave text in a ticket",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": [
    "activeTab",
    "tabs"
  ],

  "content_scripts": [{
    "matches": ["https://portal.ticketsystem.co.uk/*"],
    "js": ["popup.js"]
  }]
}
Eirik Birkeland
  • 588
  • 5
  • 11
  • Wow, I'm an idiot, this is my first attempt at an extension. I've been testing by only editing the js files and not reloading the manifest. I'll try this – jblz Sep 19 '15 at 23:42
  • Whenever you update the code files, you need to go to chrome://extensions and reload the extension. When updating the manifest itself, you have to completely remove and readd the extension. I feel your pain; Chrome is a conundrum – Eirik Birkeland Sep 19 '15 at 23:47
  • Aaaand it works. Fantastic. That's a mistake I won't be making again in a hurry. Thank you – jblz Sep 19 '15 at 23:51
  • For quick small additions / prototyping, check out Snippets in Chrome. I have been using it lately, and it's saving me a lot of CTRL-R in the Extensions screen: https://developer.chrome.com/devtools/docs/authoring-development-workflow#snippets In order to run those snippets with necessary privileges, you may have to change the filter in the top part of the console from to the name of your extension. – Eirik Birkeland Sep 19 '15 at 23:52
  • [https://jsonformatter.curiousconcept.com/](https://jsonformatter.curiousconcept.com/) is pretty alright for JSON validation. Note that JSON doesn't allow for dead-end commas, so for example you can't do `var myObj = { food: "fish", drink: "seawater", }` (this is actually allowed and encouraged in Perl's object notation for example, but JSON is very strict). Anyway, glad you solved it and good thing it wasn't something serious like a browser bug :) – Eirik Birkeland Sep 19 '15 at 23:56