简体   繁体   English

为什么此chrome扩展程序不起作用?

[英]Why doesn't this chrome extension work?

I want to collect the url (var name is 'url') of a webpage into a variable in a chrome extension, together with several user inputs in text inputs, and to send it to a remote php script for processing into an sql database. 我想将网页的url(变量名称为“ url”)收集到chrome扩展名的变量中,以及文本输入中的多个用户输入,并将其发送到远程php脚本以处理成sql数据库。 I am using AJAX to make the connection to the remote server. 我正在使用AJAX建立与远程服务器的连接。 The popup.html contains a simple form for UI, and the popup.js collects the variables and makes the AJAX connection. popup.html包含一个用于UI的简单表单,popup.js收集变量并建立AJAX连接。 If I use url = document.location.href I get the url of the popup.html, not the page url I want to process. 如果我使用url = document.location.href,则得到的是popup.html的URL,而不是我要处理的页面URL。 I tried using chrome.tabs.query() to get the lastFocusedWindow url - the script is below. 我尝试使用chrome.tabs.query()获取lastFocusedWindow网址-该脚本如下。 Nothing happens! 什么都没发生! It looks as though it should be straightforward to get lastFocusedWindow url, but it causes the script to fail. 获取lastFocusedWindow url看起来应该很简单,但是它会导致脚本失败。 The manifest.json sets 'tabs', https://ajax.googleapis.com/ , and the remote server ip (presently within the LAN) in permissions. manifest.json设置权限中的“ tabs”, https://ajax.googleapis.com/和远程服务器ip(当前在LAN内)。 The popup.html has UI for description, and some tags. popup.html具有用于描述的UI和一些标签。 (btw the response also doesn't work, but for the moment I don't mind!) (顺便说一句,响应也不起作用,但是暂时我不介意!)

//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);
});

Having no responses I first used a bookmarklet instead. 没有回应,我首先改用了书签。 The bookmarklet passes the url and title to a php script, which enters them into a db before redirecting the user back to the page they were on. 小书签将URL和标题传递给php脚本,该脚本将它们输入到db中,然后将用户重定向到他们所在的页面。

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

Then I found this code which works a treat. 然后,我发现了这段有效的代码。

var urlOutput = document.getElementById('bookmarkUrl');
var titleOutput = document.getElementById('bookmarkTitle');

if(chrome) {
chrome.tabs.query(
{active: true, currentWindow: true},
(arrayOfTabs) => { logCurrentTabData(arrayOfTabs) }
);
} else {
browser.tabs.query({active: true, currentWindow: true})
.then(logCurrentTabData)
}
const logCurrentTabData = (tabs) => {
currentTab = tabs[0];
urlOutput.value = currentTab.url;
titleOutput.value = currentTab.title;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM