vpt
vpt

Reputation: 39

Google Chrome extension that sends a image url into a specific text field

I am trying to make a google chrome extension that allows a user to click on in image in the popup window and have the images url sent to a specific websites text field; I see nothing wrong with my code but it refuses to work.

Here is my manifest.json

{
"manifest_version": 2,

"name" : "Gifs",
"description": "Gifs",
"version": "1",

"permissions": [
    "activeTab", "http://www.cssflow.com/snippets/login-form/demo", "http://*/*", "https://*/*"
],

"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
}
}

Here is my popup.html

<script src="jquery.js"></script>
<script src="popup.js"></script>
<img src="http://www.vidble.com/cayujFfA0f.gif" border="0">

<br><br>

<img src="http://www.vidble.com/U8l5gHvN5f.gif" border="0">

Here is my popup.js (the id of the text field on the website that I want to insert the urls is 'login'.)

$(document).ready(function() {
  $("img").click(function() {
    var imageurl = $(this).attr('src');
    var script = 'var form = document.getElementById("input:#login");' +
      + 'form.value = (form.value + " ' + imageurl + '");';
    chrome.tabs.executeScript({code : script});
    window.close();
  });
});

I get the following error in the console:

unexpected end of input: var form = document.getElementById("login");NaNhttp://www.vidble.com/cayujFfA0f.gif");

Any help will be greatly appreciated. Thanks in advance.

Upvotes: 0

Views: 363

Answers (1)

rsanchez
rsanchez

Reputation: 14657

Your problem is caused by an extra + in your assignment to var script. Wonders of javascript:

'a' + 'b' + 'c'
-> 'abc'

'a' + + 'b' + 'c'
-> 'aNaNc'

This is because the second expression is interpreted as 'a' + (+ 'b') + 'c', so the 'b' is coerced into a numeric value before being concatenated with the other strings.

Upvotes: 3

Related Questions