我从 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。
修改了第一个代码等待加载,但仍然像这样不稳定(我只是通过按 F5
和 shift + F5
刷新页面,下面的第二个代码。 )
// ==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/