我现在正在开发一个 chrome 扩展。但是,在开发过程中存在一个问题。
任何意见,将不胜感激。
首先,我想将 mysql(database) 与我的 chrome 扩展程序连接起来。这是因为我想实现一个函数,它获取 chrome 执行脚本页面的文本并将其与 mysql(数据库)数据进行比较。
其次,在搜索上述内容时,我发现mysql和chrome扩展之间的直接连接是不可能的,并且我必须在中间使用Web应用程序api(例如ajax或xmlhttprequest)。
第三,我决定使用ajax,下面的代码就是我写的。但是某处存在一些问题/错误,chrome 扩展程序无法正常工作。
我很好奇三件事:
- 如何更正下面编写的代码?
- 我现在用file.php,但是连接mysql的时候不能用php以外的js吗?我的意思是,在脚本 js 中,我可以在 ajax url 中使用 file.js 而不是 file.php 吗?
- 也许有一种有效的方法可以在不使用 ajax 的情况下与 mysql 连接?谢谢你。
- 所有文件都在 chrome 扩展文件夹(同一文件夹)内。
<manifest.json>
{
"name": "chrome extension name",
"description": "Build a Sample Extension",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"dependencies": {
"node-mysql": "^0.4.2",
"mysql": "^2.18.1"
},
"permissions": [
"activeTab",
"tabs",
"gcm",
"background",
"notifications",
"http://localhost/",
"<all_urls>"
],
"host_permissions": [
"https://*/*",
"http://*/*"
],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
<popup.html>
- body标签有我之前写的代码,但是我删除它并上传它,因为这个问题不需要它。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="script.js"></script>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<!--popup.html code is here-->
</body>
</html>
<script.js>
- 我先只上传ajax部分,不包括接收页面文本的代码。
- 我这样设置ajax url的原因是因为我在搜索堆栈溢出时找到了一个带有chrome扩展名的Ajax的答案。
$.ajax({
type: "POST",
url: "http://localhost.com/file.php",
data: {data:"chrome extension test"},
error:function() {
alert("error");
},
success: function(data) {
alert("success");
}
});
<文件.php>
<?php
$result = $_POST['data'];
echo $result;
?>