English Corpora Export

Add a button to export KWIC of english-corpora.org to csv file

  1. // ==UserScript==
  2. // @name English Corpora Export
  3. // @name:en English Corpora Export
  4. // @name:es Exportación de English Corpora
  5. // @name:fr Exportation de English Corpora
  6. // @name:de English Corpora Export
  7. // @name:zh-CN English Corpora 导出
  8. // @name:ja English Corpora エクスポート
  9. // @name:ar تصدير English Corpora
  10. // @name:ru Экспорт English Corpora
  11.  
  12. // @description Add a button to export KWIC of english-corpora.org to csv file
  13. // @description:en Add a button to export KWIC of english-corpora.org to csv file
  14. // @description:es Añade un botón para exportar los resultados KWIC de english-corpora.org a un archivo CSV
  15. // @description:fr Ajoute un bouton pour exporter les résultats KWIC de english-corpora.org vers un fichier CSV
  16. // @description:de Fügt eine Schaltfläche hinzu, um KWIC-Ergebnisse von english-corpora.org in eine CSV-Datei zu exportieren
  17. // @description:zh-CN 在english-corpora.org网站上添加一个按钮,将KWIC结果导出为CSV文件
  18. // @description:ja english-corpora.orgにKWIC結果をCSVファイルとしてエクスポートするボタンを追加
  19. // @description:ar إضافة زر لتصدير نتائج KWIC من english-corpora.org إلى ملف CSV
  20. // @description:ru Добавляет кнопку для экспорта результатов KWIC с english-corpora.org в файл CSV
  21.  
  22. // @namespace https://mkpo.li/
  23. // @version 0.2.0
  24. // @author mkpoli
  25. // @match https://www.english-corpora.org/coca/
  26. // @icon https://www.google.com/s2/favicons?sz=64&domain=english-corpora.org
  27. // @grant none
  28. // @license MIT
  29. // ==/UserScript==
  30.  
  31. (function () {
  32. 'use strict';
  33.  
  34. // #region Consts
  35. const BUTTON_ID = 'csv-button';
  36. const BUTTON_TEXT = 'Download CSV';
  37. // #endregion
  38.  
  39. // #region Utils
  40. function tableToCSV(table) {
  41. return Array.from(table.rows)
  42. .map((row) =>
  43. Array.from(row.cells)
  44. .map((cell) => `"${cell.textContent.trim().replace(/"/g, '""')}"`)
  45. .join(',')
  46. )
  47. .join('\n');
  48. }
  49.  
  50. function download(data, filename, type) {
  51. const blob = new Blob([data], { type });
  52. const url = URL.createObjectURL(blob);
  53. const link = document.createElement('a');
  54. link.href = url;
  55. link.download = filename;
  56. link.click();
  57. URL.revokeObjectURL(url);
  58. }
  59.  
  60. // #endregion
  61.  
  62. // #region Main
  63. console.log('[user-script] loaded');
  64. const contextFrame = parent.frames[5];
  65. console.log('[user-script] `contextFrame` found:', contextFrame);
  66.  
  67. function insertDownloadButton() {
  68. console.log('[user-script] checking for button');
  69. if (!contextFrame.document.getElementById(BUTTON_ID)) {
  70. console.log('[user-script] button not found, creating one');
  71. const contextTable = [...contextFrame.document.querySelectorAll('table')].at(-1);
  72. if (!contextTable) throw new Error('table not found');
  73. console.log('[user-script] `contextTable` found:', contextTable);
  74.  
  75. const button = contextFrame.document.createElement('button');
  76. button.textContent = BUTTON_TEXT;
  77. button.id = BUTTON_ID;
  78. button.addEventListener('click', () => {
  79. const csv = tableToCSV(contextTable).replaceAll(`"A","B","C","","","",`, '');
  80. download(csv, 'table.csv', 'text/csv');
  81. });
  82.  
  83. contextTable.parentNode.insertBefore(button, contextTable);
  84. console.log('[user-script] button added');
  85. } else {
  86. console.log('[user-script] button already exists, skipping...');
  87. }
  88. }
  89.  
  90. contextFrame.addEventListener('load', () => {
  91. console.log(`[user-script] contextFrame loaded`);
  92. insertDownloadButton();
  93. setInterval(insertDownloadButton, 5000);
  94. });
  95. // #endregion
  96. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址