将 MySQL 查询发送到我从另一个站点拥有的站点(使用 Google Chrome 扩展)
我想编写一个 Google Chrome 扩展程序,它可以从我不拥有的网站 (www.notmysite.com) 获取信息,将该信息发送到我拥有的网站 (www.mysite.com),并执行某种操作MySQL 在我的网站上查询该信息。
例如,我希望能够获取一些从 www.notmysite.com 上的 HTML 解析出来的 javascript 变量,并将其插入到 www.mysite.com 上的 MySQL 数据库中。我可以毫无问题地将 XMLHttpRequests 从一个 www.notmysite.com 页面发送到另一个页面,但当我尝试连接到 www.mysite.com 时遇到跨域脚本限制。
有办法解决这个问题吗?看来应该有,因为我拥有 www.mysite.com!
(这本质上是一个屏幕抓取问题。我想将屏幕抓取直接发送到数据库。)
I want to write a Google Chrome Extension that can take information from a site that I do not own (www.notmysite.com), send that info to a site I do own (www.mysite.com), and do some sort of MySQL query with that information on my site.
For example, I'd like to be able to take some javascript variable that I've parsed from the HTML on www.notmysite.com and INSERT it in the MySQL database on www.mysite.com. I have no problem making XMLHttpRequests from one www.notmysite.com page to another, but am running into cross-domain scripting restrictions when I try to connect to www.mysite.com.
Is there a way around this? It seems like there should be since I own www.mysite.com!
(This is essentially a screen scraping problem. I want to screen scrape straight to a database.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用称为“脚本标记注入”的技术来解决跨域限制。也就是说,您可以操作文档来插入
当您将上面所示的 a 标记插入到文档中时,浏览器将访问该 URL 来检索其中可能存在的任何内容;您可以利用“someScript”中的机会保存传递的值,或者您想做的任何其他事情。
幸运的是,您不是第一个遇到此问题的人,并且大部分艰苦的工作已经完成。看一下 jQuery 的 $.ajax() 方法,它内置了对 JSONP。这将使您能够轻松利用该技术。
You can work around cross-domain restrictions by using a technique called "script tag injection". That is, you can manipulate the document to insert a <script> tag with a "src" attribute pointing to an off-site domain, along with whatever query string parameters you wish to pass.
When you inject the a tag like the one shown above into your document, the browser will hit that URL to retrieve whatever might be there; and you can take the opportunity within "someScript" to save the passed value, or whatever else it is you wish to do.
Fortunately, you're not the first person to run into this problem, and much of the hard work has already been done. Take a look at jQuery's $.ajax() method, which has built-in support for JSONP. This will allow you to easy leverage the technique.
如果您从后台页面(而不是内容脚本)运行 ajax 调用并且在清单文件中声明了相应的域权限,则可以避免跨域限制。
You can avoid cross domain restrictions if you run your ajax call from a background page (not from a content script) and have corresponding domain permissions declared in manifest file.