stonedevil
stonedevil

Reputation: 45

Chrome.tabs.executeScript doesn't seem to be doing anything

I'm trying to learn something about javascript and came across this problem. Basically what i'm trying to do is get the url , redirect to the site in the code and place it on the textbox and activate the button Not saying all this to get answer on THAT but as a heads up as what i'm aiming for. So i'm here:

popup.js:

   var url, tab,myNewTab;
   function init(){
   chrome.tabs.query({currentWindow: true, active: true},function(tabs){
   url = tabs[0].url;
   tab = tabs[0]
   tabId = tabs[0].id;
   processTab();
    });
}

function processTab(){
chrome.tabs.update(tabId, {selected: true});
if (url.substring(0,5) === "http:")
{

    url = insert(url , 4 , "s");
}
chrome.tabs.update(tabId , { url: "https://alexanderproxy33.appspot.com"})



cont() ;
}
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
};
function cont()
{   
chrome.tabs.executeScript(null,   {code:"document.getElementById('input').value = url"});
}                             


init();

The code from the site i'm trying to access:

<form action="" method="get" accept-charset="utf-8" target="_blank">
                    <input name="url" type="text" class="txt" id="input" placeholder="type url here.." onfocus="this.value='';" />
                    <input type="submit" class="btn" value="Go" />
                </form>

I've tried some ways to get around this( i.e. experimenting with non-executeScript stuff , putting that little bit of code in file) but nothing seems to work which makes me believe that something is not working right. As i'm doing this to educate myself , any suggestions are more than welcome. Thanks in advance and know that i check here whenever i can , i might take a while , going to work

Upvotes: 0

Views: 462

Answers (1)

Xan
Xan

Reputation: 77482

You're trying to execute this in the context of the page:

chrome.tabs.executeScript(null, {
  code:"document.getElementById('input').value = url"
});

url is not defined in the page you're injecting to - resulting in what you see.

You want to evaluate url in the context of your page first:

chrome.tabs.executeScript(null, {
  // JSON-encode to wrap in quotation marks properly
  code: "document.getElementById('input').value = " + JSON.stringify(url)
});

Upvotes: 1

Related Questions