brazil_senado_federal_ecidadania.user.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. // ==UserScript==
  2. // @namespace librecitizen
  3. // @version 1.0
  4. // @description Replacements for all JavaScript of the websites for Executive, Judiciary and Legislative government powers. Configure your browser to **disable** all JavaScript.
  5. // @description:pt-BR Substitutos a todo o JavaScript dos sites dos poderes Executivo, Judiciário e Legislativo. Configure teu navegador para **desativar** todo o JavaScript.
  6. // @name LibreCitizen: Brazil Senado Federal's e-Cidadania
  7. // @name:pt-BR LibreCitizen: e-Cidadania do Senado Federal do Brasil
  8. // @match *://www12.senado.leg.br/ecidadania*
  9. // @grant none
  10. // @require https://git.savannah.nongnu.org/cgit/userjs-common.git/plain/userjs-common.js?h=1.5
  11. // ==/UserScript==
  12. /*
  13. @source https://notabug.org/adfeno/librecitizen
  14. @licstart The following is the entire license notice for the
  15. JavaScript code in this page.
  16. LibreCitizen: Replacements for all JavaScript of the websites for
  17. Executive, Judiciary and Legislative government powers.
  18. Copyright (C) 2019, 2021 Adonay Felipe Nogueira
  19. <https://libreplanet.org/wiki/User:Adfeno>
  20. <adfeno@hyperbola.info>
  21. This file is part of LibreCitizen.
  22. This program is free software: you can redistribute it and/or modify it
  23. under the terms of the GNU General Public License as published by the
  24. Free Software Foundation, either version 3 of the License, or (at your
  25. option) any later version.
  26. This program is distributed in the hope that it will be useful, but
  27. WITHOUT ANY WARRANTY; without even the implied warranty of
  28. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. General Public License for more details.
  30. You should have received a copy of the GNU Affero General Public License
  31. along with this program. If not, see <https://www.gnu.org/licenses/>.
  32. As additional permission under GNU GPL version 3 section 7, you may
  33. distribute non-source (e.g., minimized or compacted) forms of that code
  34. without the copy of the GNU GPL normally required by section 4, provided
  35. you include this license notice and a URL through which recipients can
  36. access the Corresponding Source.
  37. @licend The above is the entire license notice for the JavaScript code
  38. in this page.
  39. # Contact and contributing
  40. See <https://notabug.org/adfeno/librecitizen>.
  41. # Dependencies
  42. ## Common Userscript Definitions (userjs-common)
  43. Version: 1.5
  44. License: GPL-3.0-or-later
  45. Reference: https://git.savannah.nongnu.org/cgit/userjs-common.git/tag/?h=1.5
  46. There is no need to manually get this dependency since part of it will be
  47. downloaded through the @require property of this userscript's metadata
  48. block when installing it.
  49. */
  50. style_class_name = "librecitizen";
  51. document.head.appendChild(document.createElement("style")).innerText = `
  52. #portal-login,
  53. details > * {
  54. display: initial !important;
  55. }
  56. .collapse {
  57. border: initial !important;
  58. border-radius: initial !important;
  59. padding: initial !important;
  60. }
  61. `;
  62. window.onerror = alert;
  63. function set_csrf_protection_cookie() {
  64. /*
  65. Sets cookie for the cross-site request forgery (CSRF) protection token.
  66. */
  67. if (get_cookie("X-CSRF-TOKEN")) {
  68. return true;
  69. } else if (! document.querySelector('#protect-script')) {
  70. return false;
  71. }
  72. document.cookie = "X-CSRF-TOKEN=" +
  73. encodeURIComponent(document.querySelector('#protect-script')
  74. .getAttribute("data-token")) +
  75. ";" +
  76. "domain=" + document.location.host + ";" +
  77. "path=/ecidadania;" +
  78. "samesite=strict";
  79. return true;
  80. }
  81. function make_details_and_summary(selector_string) {
  82. document.querySelectorAll(selector_string).forEach(
  83. function replace_with_summary_and_details(anchor_element) {
  84. var old_detail_element =
  85. document.querySelector((new URL(anchor_element.href)).hash);
  86. var new_detail_element =
  87. clone_element_remvoing_event_handlers_and_listeners(
  88. old_detail_element,
  89. true
  90. );
  91. var details = new Map(
  92. [
  93. [
  94. "root",
  95. document.createDocumentFragment(),
  96. ],
  97. [
  98. "details",
  99. new Map(
  100. [
  101. [
  102. "root",
  103. document.createElement("details"),
  104. ],
  105. [
  106. "summary",
  107. new Map(
  108. [
  109. [
  110. "root",
  111. document.createElement("summary"),
  112. ],
  113. [
  114. "title",
  115. document.createElement("h2"),
  116. ],
  117. ]
  118. ),
  119. ],
  120. ]
  121. ),
  122. ],
  123. ]
  124. );
  125. details.get("details").get("summary").get("title").innerText =
  126. anchor_element.innerText;
  127. details.get("details").set(
  128. "content",
  129. new_detail_element
  130. );
  131. append_map_entries_to_root_node(details);
  132. old_detail_element.parentNode.replaceChild(
  133. details.get("root"),
  134. old_detail_element
  135. );
  136. remove_element(anchor_element);
  137. }
  138. );
  139. }
  140. async function get_comments_and_warnings() {
  141. var request = new Request(
  142. document.location.origin +
  143. "/ecidadania/ajaxcolecaocomentarioaudiencia" +
  144. "?" +
  145. "audienciaId=" +
  146. (new URLSearchParams(document.location.search)).get("id")
  147. );
  148. var response = await fetch(request);
  149. if (response.status == 200) {
  150. response = await response.text();
  151. var template = document.createElement("template");
  152. template.innerHTML = response;
  153. document.querySelector('#comentarios').appendChild(template.content);
  154. } else {
  155. console.debug(
  156. request,
  157. response,
  158. await response.text()
  159. );
  160. throw new Error(common_messages.error_unhandled_response_status);
  161. }
  162. var request = new Request(
  163. document.location.origin +
  164. "/ecidadania/ajaxmensagemavisoaudiencia" +
  165. "?" +
  166. "audienciaId=" +
  167. (new URLSearchParams(document.location.search)).get("id")
  168. );
  169. response = await fetch(request);
  170. if (response.status == 200) {
  171. response = await response.text();
  172. var template = document.createElement("template");
  173. template.innerHTML = response;
  174. document.querySelector('#box-aviso').appendChild(template.content);
  175. } else {
  176. console.debug(
  177. request,
  178. response,
  179. await response.text()
  180. );
  181. throw new Error(common_messages.error_unhandled_response_status);
  182. }
  183. }
  184. (async function keep_authenticated() {
  185. if (! set_csrf_protection_cookie()) {
  186. return false;
  187. }
  188. var request = new Request(
  189. document.location.origin +
  190. "/ecidadania/ajaxportaluser",
  191. {
  192. "method": "POST",
  193. }
  194. );
  195. var response = await fetch(request);
  196. if (response.status == 200) {
  197. // name: nome; login: e-mail
  198. response = await response.json();
  199. document.querySelector('#portal-login ins').innerText =
  200. response.name.toLowerCase();
  201. if (! localStorage.getItem("logged")) {
  202. localStorage.setItem("logged", "1");
  203. }
  204. } else {
  205. console.debug(
  206. request,
  207. response,
  208. await response.text()
  209. );
  210. throw new Error(common_messages.error_unhandled_response_status);
  211. }
  212. if (
  213. /^\/ecidadania\/visualizacaoaudiencia$/.test(document.location.pathname)
  214. ) {
  215. request = new Request(
  216. document.location.origin +
  217. "/ecidadania/ajaxautenticacaoaudiencia",
  218. {
  219. "method": "POST",
  220. "body": {
  221. "audienciaId": document.querySelector('#audienciaId').value,
  222. "_authenticator": document.querySelector('#_authenticator').value,
  223. },
  224. }
  225. );
  226. response = await fetch(request);
  227. if (response.status != 200) {
  228. console.debug(
  229. request,
  230. response,
  231. await response.text()
  232. );
  233. throw new Error(common_messages.error_unhandled_response_status);
  234. }
  235. }
  236. })();
  237. if (get_cookie("X-CSRF-TOKEN")) {
  238. for (let each_form of document.getElementsByTagName("FORM")) {
  239. var fragment_with_authentication_input =
  240. document.createDocumentFragment();
  241. var authentication_input = document.createElement("input");
  242. authentication_input.type = "hidden";
  243. authentication_input.name = "_authenticator";
  244. authentication_input.value = get_cookie("X-CSRF-TOKEN");
  245. fragment_with_authentication_input.appendChild(authentication_input);
  246. each_form.appendChild(fragment_with_authentication_input);
  247. }
  248. }
  249. if (/^\/ecidadania\/login(?:_form)?$/.test(document.location.pathname)) {
  250. var old_login_form = document.querySelector('#login_form');
  251. var new_login_form = clone_element_remvoing_event_handlers_and_listeners(
  252. old_login_form,
  253. true
  254. );
  255. document.querySelectorAll('.login-termo')
  256. .forEach(function add_term_to_login_form(old_term_element) {
  257. var new_term_element =
  258. clone_element_remvoing_event_handlers_and_listeners(
  259. old_term_element,
  260. true
  261. );
  262. new_term_element.querySelectorAll('input[type="checkbox"]')
  263. .forEach(function make_checkbox_required(input_checkbox) {
  264. input_checkbox.required = true;
  265. });
  266. new_login_form.querySelector('.formControls').parentNode.insertBefore(
  267. new_term_element,
  268. new_login_form.querySelector('.formControls')
  269. );
  270. remove_element(old_term_element);
  271. });
  272. old_login_form.parentNode.replaceChild(
  273. new_login_form,
  274. old_login_form
  275. );
  276. } else if (/^\/ecidadania\/logged_out$/.test(document.location.pathname)) {
  277. document.cookie = "X-CSRF-TOKEN=;" +
  278. "domain=" +
  279. document.location.host +
  280. ";" +
  281. "expires=" +
  282. (new Date(Date.now() - 1 * 1000)).toUTCString() +
  283. ";" +
  284. "path=/ecidadania;" +
  285. "samesite=strict";
  286. localStorage.removeItem("logged");
  287. } else if (
  288. /^\/ecidadania\/visualizacaoaudiencia$/
  289. .test(document.location.pathname)
  290. ) {
  291. get_comments_and_warnings();
  292. document.querySelectorAll('#div-comentarios form')
  293. .forEach(function replace_comment_form(old_comment_form) {
  294. new_comment_form = clone_element_remvoing_event_handlers_and_listeners(
  295. old_comment_form,
  296. true
  297. );
  298. new_comment_form.method = "post";
  299. new_comment_form.action = "ajaxcomentarioaudiencia";
  300. new_comment_form.querySelector('#comentario').name =
  301. new_comment_form.querySelector('#comentario').id;
  302. new_comment_form.querySelector('#comentario').setAttribute(
  303. "maxlength",
  304. 140
  305. );
  306. new_comment_form.querySelector('#btn-enviar').type = "submit";
  307. var audience_identity_hidden_input = document.createElement("input");
  308. audience_identity_hidden_input.type = "hidden";
  309. audience_identity_hidden_input.name = "audienciaId";
  310. audience_identity_hidden_input.value =
  311. (new URLSearchParams(document.location.search)).get("id");
  312. new_comment_form.appendChild(audience_identity_hidden_input);
  313. old_comment_form.parentNode.replaceChild(
  314. new_comment_form,
  315. old_comment_form
  316. );
  317. });
  318. document.querySelectorAll('.link-declaracao-participacao')
  319. .forEach(function replace_declaration_element(old_declaration_element) {
  320. new_declaration_element = new Map(
  321. [
  322. [
  323. "root",
  324. document.createDocumentFragment(),
  325. ],
  326. [
  327. "form",
  328. new Map(
  329. [
  330. [
  331. "root",
  332. document.createElement("form"),
  333. ],
  334. [
  335. "audience_identity",
  336. document.createElement("input"),
  337. ],
  338. [
  339. "submit_input",
  340. document.createElement("input"),
  341. ],
  342. ]
  343. ),
  344. ],
  345. ]
  346. );
  347. new_declaration_element.get("form").get("root").action =
  348. "declaracaoparticipacaoaudiencia";
  349. new_declaration_element.get("form").get("audience_identity").name =
  350. "audienciaId";
  351. new_declaration_element.get("form").get("audience_identity").type =
  352. "hidden";
  353. new_declaration_element.get("form").get("audience_identity").value =
  354. (new URLSearchParams(document.location.search)).get("id");
  355. new_declaration_element.get("form").get("submit_input").type =
  356. "submit";
  357. new_declaration_element.get("form").get("submit_input").value =
  358. old_declaration_element.innerText;
  359. if (
  360. ! localStorage.getItem("logged") ||
  361. (
  362. localStorage.getItem("logged") &&
  363. document.querySelector('#situacaoId:not([value="4"])')
  364. )
  365. ) {
  366. new_declaration_element.get("form").get("submit_input").disabled =
  367. true;
  368. }
  369. append_map_entries_to_root_node(new_declaration_element);
  370. old_declaration_element.parentNode.replaceChild(
  371. new_declaration_element.get("root"),
  372. old_declaration_element
  373. );
  374. });
  375. make_details_and_summary('#audiencia a.collapsed');
  376. } else if (
  377. /^\/ecidadania\/visualizacaoideia$/
  378. .test(document.location.pathname)
  379. ) {
  380. document.querySelectorAll('#barra-voto > .btn.ideia-btn-apoio')
  381. .forEach(function replace_unstandardized_support_button(old_button) {
  382. var new_button = new Map(
  383. [
  384. [
  385. "root",
  386. document.createDocumentFragment(),
  387. ],
  388. [
  389. "form",
  390. new Map(
  391. [
  392. [
  393. "root",
  394. document.createElement("form"),
  395. ],
  396. [
  397. "authenticator",
  398. document.querySelector('[name="_authenticator"]'),
  399. ],
  400. [
  401. "idea_identity",
  402. document.createElement("input"),
  403. ],
  404. [
  405. "submit_input",
  406. document.createElement("input"),
  407. ],
  408. ]
  409. ),
  410. ],
  411. ]
  412. );
  413. if (! new_button.get("form").get("authenticator")) {
  414. throw new Error("Não autenticado!");
  415. }
  416. new_button.get("form").set(
  417. "authenticator",
  418. new_button.get("form").get("authenticator").cloneNode(true)
  419. );
  420. new_button.get("form").get("root").method = "post";
  421. new_button.get("form").get("root").action =
  422. "ajaxapoiamentoideia";
  423. new_button.get("form").get("idea_identity").name =
  424. "id";
  425. new_button.get("form").get("idea_identity").type =
  426. "hidden";
  427. new_button.get("form").get("idea_identity").value =
  428. (new URLSearchParams(document.location.search)).get("id");
  429. new_button.get("form").get("submit_input").type =
  430. "submit";
  431. new_button.get("form").get("submit_input").value =
  432. old_button.innerText;
  433. append_map_entries_to_root_node(new_button);
  434. old_button.parentNode.replaceChild(
  435. new_button.get("root"),
  436. old_button
  437. );
  438. });
  439. make_details_and_summary('#ideia-legislativa a.collapsed');
  440. } else if (
  441. /^\/ecidadania\/visualizacaomateria$/
  442. .test(document.location.pathname)
  443. ) {
  444. var old_form;
  445. if (old_form = document.querySelector('#barra-voto')) {
  446. var new_form = new Map(
  447. [
  448. [
  449. "root",
  450. document.createDocumentFragment(),
  451. ],
  452. [
  453. "form",
  454. new Map(
  455. [
  456. [
  457. "root",
  458. document.createElement("form"),
  459. ],
  460. [
  461. "authenticator",
  462. document.querySelector('[name="_authenticator"]'),
  463. ],
  464. [
  465. "subject_identity",
  466. document.createElement("input"),
  467. ],
  468. ]
  469. ),
  470. ],
  471. ]
  472. );
  473. if (! new_form.get("form").get("authenticator")) {
  474. throw new Error("Not authenticated!");
  475. }
  476. new_form.get("form").set(
  477. "authenticator",
  478. new_form.get("form").get("authenticator").cloneNode(true)
  479. );
  480. new_form.get("form").get("root").method = "post";
  481. new_form.get("form").get("root").action = "ajaxapoiamentomateria";
  482. new_form.get("form").get("subject_identity").name = "materiaId";
  483. new_form.get("form").get("subject_identity").type = "hidden";
  484. new_form.get("form").get("subject_identity").value =
  485. (new URLSearchParams(document.location.search)).get("id");
  486. old_form.querySelectorAll('.btn[data-voto]')
  487. .forEach(function get_old_vote_button(old_button) {
  488. new_form.get("form").set(
  489. old_button.getAttribute("data-voto"),
  490. document.createElement("input")
  491. );
  492. new_form.get("form").set(
  493. old_button.getAttribute("data-voto") +
  494. "_label",
  495. document.createElement("label")
  496. );
  497. new_form.get("form").get(old_button.getAttribute("data-voto")).id =
  498. old_button.getAttribute("data-voto");
  499. new_form.get("form").get(old_button.getAttribute("data-voto")).name =
  500. "voto";
  501. new_form.get("form").get(old_button.getAttribute("data-voto")).type =
  502. "radio";
  503. new_form.get("form").get(old_button.getAttribute("data-voto")).value =
  504. old_button.getAttribute("data-voto");
  505. new_form.get("form").get(
  506. old_button.getAttribute("data-voto") +
  507. "_label"
  508. ).setAttribute(
  509. "for",
  510. old_button.getAttribute("data-voto")
  511. );
  512. new_form.get("form").get(
  513. old_button.getAttribute("data-voto") +
  514. "_label"
  515. ).innerText = old_button.innerText;
  516. });
  517. new_form.get("form").set(
  518. "submit_input",
  519. document.createElement("input")
  520. );
  521. new_form.get("form").get("submit_input").type = "submit";
  522. new_form.get("form").get("submit_input").value = common_messages.submit;
  523. append_map_entries_to_root_node(new_form);
  524. old_form.parentNode.replaceChild(
  525. new_form.get("root"),
  526. old_form
  527. );
  528. }
  529. make_details_and_summary(
  530. '#materia-legislativa a[data-toggle="collapse"], ' +
  531. '.materia-ementaExplicacao a[data-toggle="collapse"]'
  532. );
  533. }