Convert TransIP DNS to dnscontrol JSON

Provide a panel with JSON for dnscontrol on the TransIP DNS mangement page.

当前为 2021-03-22 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Convert TransIP DNS to dnscontrol JSON
  3. // @namespace http://peschar.net/
  4. // @version 1.0
  5. // @description Provide a panel with JSON for dnscontrol on the TransIP DNS mangement page.
  6. // @author Albert Peschar
  7. // @match https://www.transip.nl/cp/domein-hosting/*
  8. // @match https://www.transip.eu/cp/domain-hosting/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. 'use strict';
  13.  
  14. const RECORD_LIST_SELECTOR = '#dnsEntries';
  15.  
  16. let reentrant = false;
  17. let existingPanel;
  18.  
  19. (new MutationObserver(function (mutations) {
  20. if (reentrant) {
  21. return;
  22. }
  23. mutations.forEach(mutation => {
  24. const recordList =
  25. mutation.target.querySelector(RECORD_LIST_SELECTOR) ||
  26. mutation.target.closest(RECORD_LIST_SELECTOR);
  27. if (recordList) {
  28. reentrant = true;
  29. try {
  30. updateExport(recordList);
  31. } finally {
  32. Promise.resolve().then(() => {reentrant = false});
  33. }
  34. }
  35. });
  36. })).observe(document.documentElement, {
  37. subtree: true,
  38. childList: true
  39. });
  40.  
  41. function updateExport(recordList) {
  42. const result = [];
  43. recordList.querySelectorAll(':scope > .dns-form-panels > .form-panel').forEach(record => {
  44. const name = record.querySelector('input.name').value;
  45. if (name == '') {
  46. return;
  47. }
  48. const type = record.querySelector('select.type').value;
  49. const content = record.querySelector('input.content').value;
  50. let match, args;
  51. if (type == 'MX' && (match = /^(\d+)\s+(.+)$/.exec(content))) {
  52. args = [name, parseInt(match[1]), match[2]];
  53. } else {
  54. args = [name, content];
  55. }
  56. result.push(` ${type}(${args.map(JSON.stringify).join(', ')}),\n`);
  57. });
  58.  
  59. const panel = document.createElement('div');
  60. panel.className = 'overview-panel ocp-text';
  61.  
  62. const pre = document.createElement('pre');
  63. pre.innerText = `[\n${result.join('')}]`;
  64. panel.appendChild(pre);
  65.  
  66. if (existingPanel) {
  67. existingPanel.remove();
  68. }
  69.  
  70. const afterPanel = recordList.closest('.overview-panel');
  71. afterPanel.parentNode.insertBefore(panel, afterPanel.nextSibling);
  72.  
  73. existingPanel = panel;
  74. }