通过浏览器扩展添加表值

发布于 2024-10-12 18:20:49 字数 406 浏览 3 评论 0原文

我在网站表中有一组数据,该表有多个页面,我需要解析并添加特定字段的值,特别是价格字段和库存字段。 我无权访问代码或数据库。有谁知道 Chrome 或 Firefox 中是否有扩展程序可以促进此类功能?这是我需要的数据示例:

我想要一个非常简单的摘要例如“库存总价:$500.00”。我什至不介意它是否是每页的,我必须写下每个新的总数并自己将它们加起来。我精通 jQuery 等,所以如果我能自己写这个,我什至不介意指向那个方向的指针。但它必须是一个扩展,因为我无法直接访问该页面。

编辑:greasemonkey 脚本可以做到这一点吗?

I have a set of data in a website table that has multiple pages that I need to parse and add the values of a certain field, specifically price field and an inventory field. I don't have access to the code or database. Does anyone know if there is an extension in Chrome or Firefox that can facilitate this type of functionality? Here is an example of the data I need:

I would like a very simple summary like "Total Inventory Price: $500.00". I wouldn't even mind if it were per page and I had to write down each new total and add them up myself. I am proficient in jQuery and such so if I could somehow write this myself, I wouldn't even mind a pointer in that direction. It would have to be an extension though as I don't have direct access to the page.

Edit: Could a greasemonkey script do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙温凉少女 2024-10-19 18:20:49

如果您精通 jQuery,并且知道如何从该表中提取数据并进行自己的计算,那么这对您来说将非常容易!请参阅文档,它包含执行此类操作所需的所有信息:

http:// /code.google.com/chrome/extensions/getstarted.html

一种方法

如果您希望在访问该页面时始终显示总计,那么您可以使用 内容脚本。内容脚本世界将直接与该页面的 DOM 通信,因此您可以使用 jQuery 来完成您的任务。其骨架为:

ma​​nifest.json

{
  "name": "Content Script test",
  "version": "0.1",
  "description": "Content Script test",
  "content_scripts": [
    {
      "matches": ["http://www.website.com/*"],
      "js": [ "jquery-1.4.2.min.js", "cs.js" ],
      "run_at": "document_start",
      "all_frames": true
    }
  ]
}

cs.js

// Your jQuery

另一种方法,

如果您只想在单击浏览器上的按钮时显示总计。您可以使用浏览器操作。您需要一个后台页面来监听您点击该浏览器操作的情况。基本上,其框架是:

ma​​nifest.json

{
  "name": "Browser Action test",
  "version": "0.1",
  "description": "Content Script test",
  "background_page": "background.html",
    "browser_action": {
    "default_icon": "icon19.png",
    "default_title": "Browser Action Test"
  },
  "permissions": [ "tabs", "http://www.website.com/*" ]
}

background.html

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(tab.id, {file: 'jquery-1.4.2.min.js'});
  chrome.tabs.executeScript(tab.id, {file: 'cs.js'});
});

cs.js

// Your jQuery

就是这样,请参阅文档以获取更多帮助,上面的代码未经测试,所以如果东西不能立即工作,请不要感到惊讶:)

If you're proficient in jQuery, and know how to extract the data from that table and do your own calculations, then it will be super easy for you to do! Please refer to the documentation, it has all the information you need to do such thing:

http://code.google.com/chrome/extensions/getstarted.html

One Way

If you want this to always show the totals when you visit that page, then you can use a content script. The content script world will have direct communication to the DOM of that page, so you can use jQuery and do your thing. Skeleton for that would be:

manifest.json

{
  "name": "Content Script test",
  "version": "0.1",
  "description": "Content Script test",
  "content_scripts": [
    {
      "matches": ["http://www.website.com/*"],
      "js": [ "jquery-1.4.2.min.js", "cs.js" ],
      "run_at": "document_start",
      "all_frames": true
    }
  ]
}

cs.js

// Your jQuery

Another Way

If you want to show the totals only when you click on a button on the browser. You can use a browser action. You would need a background page to listen when you click on that browser action. Basically, the skeleton for that would be:

manifest.json

{
  "name": "Browser Action test",
  "version": "0.1",
  "description": "Content Script test",
  "background_page": "background.html",
    "browser_action": {
    "default_icon": "icon19.png",
    "default_title": "Browser Action Test"
  },
  "permissions": [ "tabs", "http://www.website.com/*" ]
}

background.html

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(tab.id, {file: 'jquery-1.4.2.min.js'});
  chrome.tabs.executeScript(tab.id, {file: 'cs.js'});
});

cs.js

// Your jQuery

That's it, please refer to the documentation for more assistance, the code above is untested, so don't be surprised if stuff don't work right out of the box :)

不知在何时 2024-10-19 18:20:49

您可以尝试类似的操作,并将其另存为书签:

$('table.inventory').each( function(i){ // allow possibility of more than one table?
  var arTot = $(this).find('tr').map( function(i,e){
    var qty = parseInt( e.find('td:first').text(), 10 );
    var price = parseFloat( e.find('td:last').text() );
    return qty * price; // leave rounding and precision as your exercise
  }).get(); // result as an array
  alert( 'Total: ' + arraySum( arTot ) ); // reduce to single value
});

function arraySum( ar ){ // convenience function
  for(var i=0, sum=0, len=ar.length; i<len; sum+=[i++]);
  return sum;
}

未经测试,但应该为您提供总体思路。使用小书签生成器(有 很多),放入 JavaScript,并将其另存为书签。然后,只需在查看任何页面时单击小书签链接,您就会收到显示总数的警报。可能的增强功能包括使用 URL 作为键将值保存到 localStorage(这样您就不会重复,并且可以跟踪累积总数)。

You can try something like this, and save it as a bookmarklet:

$('table.inventory').each( function(i){ // allow possibility of more than one table?
  var arTot = $(this).find('tr').map( function(i,e){
    var qty = parseInt( e.find('td:first').text(), 10 );
    var price = parseFloat( e.find('td:last').text() );
    return qty * price; // leave rounding and precision as your exercise
  }).get(); // result as an array
  alert( 'Total: ' + arraySum( arTot ) ); // reduce to single value
});

function arraySum( ar ){ // convenience function
  for(var i=0, sum=0, len=ar.length; i<len; sum+=[i++]);
  return sum;
}

This is not tested, but should give you the general idea. Use a bookmarklet generator (there are many), drop in your JavaScript, and save it as a bookmarklet. Then just click the bookmarklet link while viewing any page, and you'll get an alert displaying the total. Possible enhancements include saving the value to localStorage using the URL as a key (so you don't duplicate, and can track cumulative totals).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文