这是我的 popup.html:
<!DOCTYPE html>
<html lang="en">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="script.js"></script>
<script src="popup.js"></script>
<head>
<meta charset="utf-8">
<title> Chrome Extension</title>
<link rel="stylesheet" href="style.css" />
</head>
<body id="container">
<div id="left">
<form>
<div class="input-wrapper">
<input type="text" id="query" />
<button id="openBackgroundWindow">Open background window</button>
</div>
<input type="submit" id="button" value="Search" alt="Loading" />
</form>
<!-- rest of the code will follow this -->
<!-- other code above -->
<div id="results">
<ul id="results-list">
<!-- this gets populated -->
</ul>
</div>
</div> <!-- end #left -->
</body>
</html>
这是我的 popup.js:
$("#openBackgroundWindow").click(function(){
login();
chrome.extension.sendRequest({ msg: "login" });
});
function openNextPage(){
var bg = chrome.extension.getBackgroundPage().openNextPage();
}
function pageRender(){
console.log("no user")
if (localStorage.getItem("username") == null){
console.log("no user")
}
function codeAddress() {
document.getElementById('button').style.visibility='hidden';
}
}
function login(){
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/login", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// JSON.parse does not evaluate the attacker's scripts.
var resp = JSON.parse(xhr.responseText);
}
}
xhr.send();
}
这是我的背景.js:
function login(){
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:3000/login", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// JSON.parse does not evaluate the attacker's scripts.
var resp = JSON.parse(xhr.responseText);
}
}
xhr.send();
}
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse){
print "hello";
if(request.msg == "login") login();
}
);
既没有调用 background.js 中的 login() 函数,也没有调用 popup.js 中的 login 函数。可能是什么问题?