javascript - adding the selected text on the webpage inside a field in an injected sidebar chrome extension when clicked -


hi have chrome extension injects sidebar when clicked.what want if select location on webpage , click on extension, automatically fills location input bar selected text , call maps api inside iframe location name query.i have tried using window.getselection().tostring() inside script.js gives me empty string on console log. new chrome extension, not have hang on how the selected text. appreciated. thanks

script.js

var place;  function handlerequest(request, sender, sendresponse) {     if (request.method == "getselection")        //sendresponse({data: });      if (request.callfunction == "togglesidebar") {           togglesidebar();           console.log("adasda");           place = window.getselection().tostring();     } }  chrome.extension.onrequest.addlistener(handlerequest);  var sidebaropen = false; function togglesidebar() {     if(sidebaropen) {         var el = document.getelementbyid('mysidebar');         el.parentnode.removechild(el);         sidebaropen = false;     }     else {          console.log("hello");         console.log(place);          var sidebar = document.createelement('div');         sidebar.id = "mysidebar";         sidebar.innerhtml = "<form id='addbookmark'><p><label for='title'>" + "location" + "</label><br /></form><input type='text' id='title' name='title' size='20' value='' /></p><p><label for='summary'>" + "description" + "</label><br /></form><p><label for='summary'>" + "image" + "</label><br /><input type='text' id='summary' name='summary' size='20' value='' /></p><input type='text' id='image' name='image' size='20' value='' /><iframe width = '500' height = '310' src='https://www.google.com/maps/embed/v1/place?key=aizasycpkknjmevis7cvjvilgad83vl5igu_dfc&q=singapore' allowfullscreen></iframe><div id='dropbox' ondrop='drop(event);' ondragover='return false' ><img id ='myimage' style = 'max-width:300px;max-height:300px' src = ''></div>";  //      '\ //          <h1>hello</h1>\ //          <form id=" '+ addbookmark + '">\ //          ';          sidebar.style.csstext = "\             position:fixed;\             top:0px;\             right:0px;\             width:23%;\             height:70%;\             background:white;\             box-shadow:inset 0 0 1em black;\             z-index:999999;\         ";          document.body.appendchild(sidebar);         sidebaropen = true;     } }   var dropbox = document.getelementbyid('dropbox'); dropbox.addeventlistener('drop', drop, false);   function drop(evt) {     evt.stoppropagation();     evt.preventdefault();      var imageurl = evt.datatransfer.getdata('url'); <!--    alert(imageurl)-->     document.getelementbyid('myimage').src = imageurl; } 

background.html

    <script type="text/javascript" src="background.js"></script> 

background.js

console.log( 'background.html starting!' ); /*put page action icon on tabs*/ chrome.tabs.onupdated.addlistener(     function(tabid) {             chrome.pageaction.show(tabid);         });  chrome.tabs.getselected(null, function(tab) {     chrome.pageaction.show(tab.id); });  /*send request current tab when page action clicked*/ chrome.pageaction.onclicked.addlistener(     function(tab) {         chrome.tabs.getselected(null, function(tab) {             chrome.tabs.sendrequest(                 //selected tab id                 tab.id,                 //params inside object data                 {method : "getselection",                 callfunction: "togglesidebar"},                  //optional callback function                 function(response) {                     console.log(response.data);                 }             );         });     } );  var place = window.getselection().tostring()  console.log( 'background.html done.' ); 

manifest.json

{     "name": "inject sidebar",     "manifest_version": 2,     "description": "inject sidebar on current tab when page action clicked",     "version": "0.2",     "background": {         "page": "background.html"     },     "page_action": {         "default_icon": "icon.png",         "default_title": "inject sidebar"     },     "content_scripts": [         {             "matches": ["<all_urls>"],             "js": ["script.js"]         }     ],     "permissions": [         "tabs"     ] } 

i have seen problems in code.

background.js

var place = window.getselection().tostring() should not placed here. can't have selected text in background page. can remove line.

like said in comment, should replace depracated chrome.tabs.getselected chrome.tabs.query({active: true}, callback);, read more function here.

script.js

the smelling code handlerequest function. have made condition, first doing nothing, , second, if have understood, displaying sidebar.

so think problem here. should move place = window.getselection().tostring(); statement first condition , call sendresponse({data : place}). way, should send text selected in page background script.

function handlerequest(request, sender, sendresponse) {     if (request.method == "getselection")     {        place = window.getselection().tostring();        console.log("sending background : " + place);        sendresponse({data: place});     }      if (request.callfunction == "togglesidebar")     {        togglesidebar();        console.log("adasda");     } } 

all code

you should clean up. there lot of not usefull comment should remove. improve readability of code better debuging.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -