2017-02-02 15 views
0

ウェブページのurl(var名は 'url')をクロム拡張の変数に、テキスト入力の複数のユーザー入力と共に収集し、リモートphpスクリプトに送信してsqlに処理したいデータベース。私はAJAXを使ってリモートサーバに接続しています。 popup.htmlにはUI用の簡単なフォームがあり、popup.jsは変数を収集してAJAX接続を行います。 url = document.location.hrefを使用すると、処理したいページURLではなく、popup.htmlのURLが取得されます。私はlastFocusedWindow URLを取得するためにchrome.tabs.query()を使ってみました - スクリプトは以下の通りです。何も起こりません! lastFocusedWindow urlを取得するのは簡単ですが、スクリプトが失敗するように見えます。 manifest.jsonは、 'tabs'、https://ajax.googleapis.com/、およびリモートサーバのIP(現在LAN内にある)をパーミッションで設定します。 popup.htmlには説明用のUIといくつかのタグがあります。 (応答もうまくいきませんが、現時点では気にしません)なぜこのChrome拡張機能が機能しないのですか?

//declare variables to be used globally 
var url; 

// Get the HTTP Object 
function getHTTPObject(){ 
if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP"); 
else if (window.XMLHttpRequest) return new XMLHttpRequest(); 
else { 
alert("Your browser does not support AJAX."); 
    return null; 
} 
// Change the value of the outputText field THIS PART IS NOT WORKING YET 
function setOutput(){ 
if(httpObject.readyState == 4){ 
    //document.getElementById('outputText').value = httpObject.responseText; 
"Bookmark added to db" = httpObject.responseText; // does this work?  
} 
} 
//put URL tab function here 
chrome.tabs.query(
{"active": true, "lastFocusedWindow": true}, 
    function (tabs) 
    { 
     var url = tabs[0].url; //may need to put 'var' in front of 'url' 
    } 
); 
// Implement business logic  
function doWork(){  
    httpObject = getHTTPObject(); 
if (httpObject != null) { 
//get url? THIS IS OUTSTANDING - url defined from chrome.tabs.query? 
description = document.getElementById('description').value; 
tag1 = document.getElementById('tag1').value; 
tag2 = document.getElementById('tag2').value; 
tag3 = document.getElementById('tag3').value; 
tag4 = document.getElementById('tag4').value; 
    httpObject.open("GET", "http://192.168.1.90/working/ajax.php?url="+url+"&description="+description+"&tag1="+tag1+"&tag2="+tag2+"&tag3="+tag3+"&tag4="+tag4, true); 
    httpObject.send(null); 
    httpObject.onreadystatechange = setOutput(); //THIS PART IS NOT WORKING 
finalString = httpObject.responseText; //NOT WORKING 
return finalString; //not working 
} //close if 
} //close doWork function 
var httpObject = null; 
var url = null; 
var description = null; 
var tag1 = null; 
var tag2 = null; 
var tag3 = null; 
var tag4 = null;  
// listens for button click on popup.html 
document.addEventListener('DOMContentLoaded', function() { 
document.querySelector('button').addEventListener('click', doWork); 
}); 

答えて

1

私はブックマークレットを代わりに使用しました。ブックマークレットはurlとtitleをphpスクリプトに渡します。phpスクリプトは、ユーザをリダイレクトしてから、そのページに戻します。

javascript:(function(){location.href='http://[ipaddress]/bookmarklet.php?url='+encodeURIComponent(location.href)+'&description='+encodeURIComponent(document.title)})() 
関連する問題