簡體   English   中英

為什么此chrome擴展程序不起作用?

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

我想將網頁的url(變量名稱為“ 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);
});

沒有回應,我首先改用了書簽。 小書簽將URL和標題傳遞給php腳本,該腳本將它們輸入到db中,然后將用戶重定向到他們所在的頁面。

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

然后,我發現了這段有效的代碼。

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