doumei1955 2015-06-27 11:13
浏览 57

如何从数据库中检索信息以使用Chrome扩展程序显示

I am trying to write my first chrome extension. The workflow goes something like this -When the extension is installed and active if a user hovers over a specific product/ID displayed on the page, the extension retrieves related vendor data about the product with the ID.

This is how I thought about this:

  1. Use jQuery attr to access the ID on mouse over.
  2. Post this ID to a retrieve.php file with .post() method
  3. The retrieve.php file retrieves the data from database
  4. Display the data in a tool tip on the web page.

I have some queries for the above process:

  1. I am able to get this working on a local XAMPP server but how will it work online as the chrome extension will not have access to server. What is the way around to retrieve data without using PHP?

  2. I am able to get the logic working but am unable to place these in respective files - Will all my logic reside in background.js ?

Any suggestions on getting this started will be much appreciated.

  • 写回答

1条回答 默认 最新

  • duanshan2988 2015-06-27 11:39
    关注

    You could build a very simple API on your server that responds with JSON to any request it receives after processing it. Like this:

    {"firstVar":"foo","secondVar":"bar" }
    

    Your chrome extension can then make an xmlhttp request to this server and and process the returned data.(You could also use JSONP and wrap the response in a callback function which will execute as soon as you have the reponse)

    The JS extension will be able to deal with the JSON nicely as it can understand that format so you can then choose to display the data in whatever way you want.

    Essentially, what you want is a server that can take an ID posted to it and return the corresponding date in a nice and readable format. And a chrome extension that can make an request to a server and then process the response. Build and test them separately (keep positing an ID to the server and see the response and for your JS side at first instead of making requests to your unfinished API just set a static response to begin with which will be the same as an expected response.

    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多