为什么这个chrome扩展不起作用?

时间:2017-02-02 16:36:26

标签: javascript ajax google-chrome tabs

我想将网页的网址(var名称为'url')收集到chrome扩展中的变量中,以及文本输入中的几个用户输入,并将其发送到远程php脚本以进行处理sql数据库。我正在使用AJAX建立与远程服务器的连接。 popup.html包含一个简单的UI表单,popup.js收集变量并建立AJAX连接。如果我使用url = document.location.href我得到popup.html的url,而不是我要处理的页面url。我尝试使用chrome.tabs.query()来获取lastFocusedWindow网址 - 脚本如下。什么都没发生!看起来应该很容易获得lastFocusedWindow url,但它会导致脚本失败。 manifest.json在权限中设置'tabs',https://ajax.googleapis.com/和远程服务器ip(目前在LAN内)。 popup.html具有用于描述的UI和一些标签。 (顺便说一下,回复也不起作用,但目前我不介意!)

//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 个答案:

答案 0 :(得分:1)

没有回复我首先使用了书签。 bookmarklet将url和title传递给php脚本,在将用户重定向回他们所在的页面之前将其输入到db中。

import tensorflow as tf 

kh = tf.random_uniform(
    shape= [5,4],
    maxval=67,
    dtype=tf.int32,
    seed=None,
    name=None
)

kh = tf.cast(kh, tf.float32)

sess = tf.InteractiveSession()

softmaxkh = tf.nn.softmax(kh)

# produces consistent output values
print(sess.run([kh, softmaxkh))
# also produces consistent values, but different from above
print(sess.run([kh, softmaxkh))

然后我发现这个代码有效。

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