starling_dmp_i18n

try to take over the world!

  1. // ==UserScript==
  2. // @name starling_dmp_i18n
  3. // @namespace starling_dmp_i18n
  4. // @version 0.7
  5. // @description try to take over the world!
  6. // @author lejunjie
  7. // @match https://ads.tiktok.com/insight*
  8. // @match https://ads.tiktok.com/i18n_dmp/adver/*
  9. // @match https://ads.tiktok.com/quicksurvey/*
  10. // @match http://localhost:8889/i18n_dmp/adver/*
  11. // @match http://localhost:9417/insight*
  12. // @match http://localhost:9417/quicksurvey/*
  13. // @grant GM_xmlhttpRequest
  14. // ==/UserScript==
  15.  
  16. (function() {
  17. 'use strict';
  18. const langMatch = document.cookie.match(/lang_type=([a-zA-Z]*);/);
  19. const href = window.location.href;
  20.  
  21. const isDmp = href.includes('/i18n_dmp/adver');
  22. const isInsight = href.includes('/insight');
  23. const isSurvey = href.includes('/quicksurvey');
  24.  
  25. const namespace = isDmp ? [476, 266] : isInsight ? [3461, 813] : isSurvey ? [3443, 809] : [0, 0];
  26. if (!namespace[0] || !namespace[1]) {
  27. return;
  28. }
  29.  
  30. function clearSelection() {
  31. if (window.getSelection) {
  32. if (window.getSelection().empty) {
  33. window.getSelection().empty();
  34. } else if (window.getSelection().removeAllRanges) {
  35. window.getSelection().removeAllRanges();
  36. }
  37. } else if (document.selection) {
  38. document.selection.empty();
  39. }
  40. }
  41. document.body.addEventListener('click', function(e) {
  42. if (e.target.id === 'starling_close') {
  43. const el = document.querySelector('#starling_ljj');
  44. if (el) {
  45. el.remove();
  46. }
  47. }
  48. }, false);
  49.  
  50. function popup(title, text) {
  51. const el = document.querySelector('starling_ljj');
  52. if (!el) {
  53. const html = `
  54.  
  55. <div id="starling_content" style="top: 0; position: absolute; width: 480px; height: 300px; background-color: #fff; left: 50%; top: 50%; transform: translate(-50%, -50%);overflow: scroll;padding: 24px;">
  56. <h3 id="starling_content_title" style="display: flex;justify-content: space-between;">
  57. <span>${title}</span>
  58. <span id="starling_close">X</span>
  59. </h3>
  60. <div id="starling_content_text">${text}</div>
  61. </div>
  62. `;
  63. const div = document.createElement('div');
  64. div.style = "position:fixed; width: 100%; height: 100%; background-color: rgba(red, green, blue, .1); z-index: 9999;background: rgba(0,0,0,.2);top: 0;";
  65. div.id = "starling_ljj";
  66. div.innerHTML = html;
  67. document.body.appendChild(div);
  68. }
  69. }
  70.  
  71. GM_xmlhttpRequest({
  72. method: "GET",
  73. url: `https://starling.bytedance.net/text/getTextListWithKeyOrSource?namespaceId=${namespace[0]}&locale=${langMatch && langMatch[1] || 'en'}&offset=0&limit=2000&projectId=${namespace[1]}`,
  74. onload: function(response) {
  75. //这里写处理函数
  76.  
  77. if (response && response.readyState === 4 && response.responseText) {
  78. const data = JSON.parse(response.responseText || '{}');
  79. let sources = (data.data && data.data.textList || []).map(item => ({
  80. key: item.targetText && item.targetText.key,
  81. content: item.targetText && item.targetText.content
  82. })).filter(item => item.key && item.content);
  83. function selectCallback(innerText, startOffset, endOffset) {
  84. const text = innerText.slice(startOffset, endOffset);
  85. const result = sources.filter(item => (item.content || '').includes(text));
  86.  
  87. if (result.length && text) {
  88. clearSelection();
  89. setTimeout(() => {
  90. popup('source text: ' + text + '\n\n', result.map(item => item.key + ':' + item.content).join('<br>'));
  91. }, 200)
  92. }
  93. }
  94.  
  95. var handler;
  96.  
  97. if (typeof window.getSelection != "undefined") {
  98. // Non-IE
  99. handler = function() {
  100. var sel = window.getSelection();
  101. if (sel.rangeCount > 0) {
  102. var range = sel.getRangeAt(0);
  103. if (range.toString()) {
  104. var selParentEl = range.commonAncestorContainer;
  105. var selText = selParentEl.innerText;
  106. if (selParentEl.nodeType == 3) {
  107. selText = selParentEl.textContent;
  108. if (selParentEl.parentNode.id === 'starling_content_text' || (selParentEl.parentNode.parentNode && selParentEl.parentNode.parentNode.id === 'starling_content_title')) {
  109. return;
  110. }
  111. }
  112. if (selParentEl.id === 'starling_content') {
  113. return;
  114. }
  115. selectCallback(selText, range.startOffset, range.endOffset);
  116. }
  117. }
  118. };
  119. } else if (typeof document.selection != "undefined") {
  120. // IE
  121. handler = function() {
  122. var sel = document.selection;
  123. if (sel.type == "Text") {
  124. var textRange = sel.createRange();
  125. if (textRange.text != "") {
  126. selectCallback(textRange.text, 0, textRange.text.length);
  127. }
  128. }
  129. };
  130. }
  131. document.addEventListener('mouseup', handler, false);
  132. }
  133. }
  134. });
  135. })();