install-hook-usercss.js 1.0 KB

1234567891011121314151617181920212223242526
  1. 'use strict';
  2. // preventing reregistration if reinjected by tabs.executeScript for whatever reason, just in case
  3. if (typeof window.oldCode !== 'string') {
  4. window.oldCode = (document.querySelector('body > pre') || document.body).textContent;
  5. chrome.runtime.onConnect.addListener(port => {
  6. if (port.name !== 'downloadSelf') return;
  7. port.onMessage.addListener(async ({id, force}) => {
  8. const msg = {id};
  9. try {
  10. const code = await (await fetch(location.href, {mode: 'same-origin'})).text();
  11. if (code !== window.oldCode || force) {
  12. msg.code = window.oldCode = code;
  13. }
  14. } catch (error) {
  15. msg.error = error.message || `${error}`;
  16. }
  17. port.postMessage(msg);
  18. });
  19. // FF keeps content scripts connected on navigation https://github.com/openstyles/stylus/issues/864
  20. addEventListener('pagehide', () => port.disconnect(), {once: true});
  21. });
  22. }
  23. // passing the result to tabs.executeScript
  24. window.oldCode; // eslint-disable-line no-unused-expressions