javascript - 加载 CSS 未 100% 工作

标签 javascript css

我从 this question 得到了下面的代码,该脚本用于在 Google tasks 上更改 iframe[src="about:blank"] 内的 CSS使用 Chrome 扩展 Tempermonkey。

// ==UserScript==
// @name    _Style iframe with src="about:blank"
// @match   https://mail.google.com/tasks/canvas
// ==/UserScript==

var targetFrame = document.querySelector("iframe[src='about:blank']");

  if (targetFrame) {
    addStyleToFrame(
      `* {color: red !important}`, targetFrame
    );


    function addStyleToFrame(cssStr, frmNode) {
      var D = frmNode.contentDocument;
      var newNode = D.createElement('style');
      newNode.textContent = cssStr;

      var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
      targ.appendChild(newNode);
    }
  }

此代码有时会将字体颜色更改为红色但有时不会,如何使此代码 100% 工作?


镜头 2。 修改了第一个代码等待加载,但仍然像这样不稳定(我只是通过按 F5shift + F5 刷新页面,下面的第二个代码。 ) It's just refreshing the page.

// ==UserScript==
// @name    _Style iframe with src="about:blank"
// @match   https://mail.google.com/tasks/canvas
// ==/UserScript==

var targetFrame = document.querySelector("iframe[src='about:blank']");
document.querySelector("iframe[src='about:blank']").onload = function() {
  if (targetFrame) {
    addStyleToFrame(
      `* {color: red !important}`, targetFrame
    );


    function addStyleToFrame(cssStr, frmNode) {
      var D = frmNode.contentDocument;
      var newNode = D.createElement('style');
      newNode.textContent = cssStr;

      var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement;
      targ.appendChild(newNode);
    }
  }}

最佳答案

修改为这样解决。 我感到很羞耻,以至于我每次都不得不继续询问可能是最基本的技能。第一个代码由 Brock Adams 制作在 here 上发帖从下到上。

答案1.添加Wait For Key Element.js

// ==UserScript==
// @name     _Style iframe with src="about:blank"
// @match    https://mail.google.com/tasks/canvas
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// @grant    GM.getValue
// ==/UserScript==
//- The @grant directives are needed to restore the proper sandbox.

const desiredStyles = `* {color: red !important;}`;

waitForKeyElements (
    ".goog-toolbar:first",
    addCSS_Style,
    false,
    "iframe[src='about:blank']"
);

function addCSS_Style (jNode) {
    var frmBody = jNode.closest ("body");
    //-- Optionally add a check here to avoid duplicate <style> nodes.
    frmBody.append (`<style>${desiredStyles}</style>`);
}

答案 2。通过 wOxxOm on forum.userstyles.org 使用 Mutation Observer

// ==UserScript==
// @name     tasks
// @match    https://mail.google.com/tasks/canvas
// @run-at   document-start
// ==/UserScript==

const css = `
  * {color: red !important}
`;

addStyle(css);

new MutationObserver((_, observer) => {
  const iframe = document.getElementsByTagName('iframe')[0];
  if (iframe) {
    observer.disconnect();
    iframe.addEventListener('load', () => addStyle(css, iframe), {once: true});
  }
}).observe(document, {subtree: true, childList: true});

function addStyle(css, frame = window) {
  const doc = frame.contentDocument || frame.document;
  const el = document.createElement('style');
  el.textContent = css;
  doc.documentElement.appendChild(el);
}

关于javascript - 加载 CSS 未 100% 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51798363/

相关文章:

javascript - 在 HTML 上方绘制箭头

javascript - 通过索引而不是名称获取 json 键和值

html - css:如何强制图像以相同的比例缩放?

css - 动态设置div宽度和高度的方法

css - CORS(Access-Control-Allow-Origin header )如何提高安全性?

javascript - 光标在 ngModelChange Angular/Typescript 上结束时出现问题

javascript - 当前在谷歌地图中查看的国家/地区

javascript - 使用 ng-click 函数激活 $interval

css - 悬停在另一个 div 上时更改颜色

html - 带彩色边栏和垂直文本的 Accordion 幻灯片