2018-05-25 17:22:14 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2023-03-17 01:55:59 +00:00
|
|
|
export class PageStyleChild extends JSWindowActorChild {
|
2023-02-24 21:09:36 +00:00
|
|
|
actorCreated() {
|
|
|
|
// C++ can create the actor and call us here once an "interesting" link
|
|
|
|
// element gets added to the DOM. If pageload hasn't finished yet, just
|
|
|
|
// wait for that by doing nothing; the actor registration event
|
|
|
|
// listeners will ensure we get the pageshow event.
|
|
|
|
// It is also possible we get created in response to the parent
|
|
|
|
// sending us a message - in that case, it's still worth doing the
|
|
|
|
// same things here:
|
|
|
|
if (!this.browsingContext || !this.browsingContext.associatedWindow) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let { document } = this.browsingContext.associatedWindow;
|
|
|
|
if (document.readyState != "complete") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If we've already seen a pageshow, send stylesheets now:
|
|
|
|
this.#collectAndSendSheets();
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:08:02 +00:00
|
|
|
handleEvent(event) {
|
2023-02-24 21:09:36 +00:00
|
|
|
if (event?.type != "pageshow") {
|
|
|
|
throw new Error("Unexpected event!");
|
|
|
|
}
|
2019-10-24 20:51:59 +00:00
|
|
|
|
2023-02-24 21:09:36 +00:00
|
|
|
// On page show, tell the parent all of the stylesheets this document
|
|
|
|
// has. If we are in the topmost browsing context, delete the stylesheets
|
|
|
|
// from the previous page.
|
|
|
|
if (this.browsingContext.top === this.browsingContext) {
|
|
|
|
this.sendAsyncMessage("PageStyle:Clear");
|
2019-10-27 18:08:02 +00:00
|
|
|
}
|
2023-02-24 21:09:36 +00:00
|
|
|
|
|
|
|
this.#collectAndSendSheets();
|
2018-07-30 02:54:17 +00:00
|
|
|
}
|
2018-05-25 17:22:14 +00:00
|
|
|
|
|
|
|
receiveMessage(msg) {
|
|
|
|
switch (msg.name) {
|
2019-10-27 18:08:02 +00:00
|
|
|
// Sent when the page's enabled style sheet is changed.
|
2018-05-25 17:22:14 +00:00
|
|
|
case "PageStyle:Switch":
|
2020-10-04 03:25:16 +00:00
|
|
|
if (this.browsingContext.top == this.browsingContext) {
|
|
|
|
this.browsingContext.authorStyleDisabledDefault = false;
|
|
|
|
}
|
2023-11-24 09:10:52 +00:00
|
|
|
this.docShell.docViewer.authorStyleDisabled = false;
|
2019-10-27 18:08:02 +00:00
|
|
|
this._switchStylesheet(msg.data.title);
|
2018-05-25 17:22:14 +00:00
|
|
|
break;
|
2019-10-27 18:08:02 +00:00
|
|
|
// Sent when "No Style" is chosen.
|
2018-05-25 17:22:14 +00:00
|
|
|
case "PageStyle:Disable":
|
2020-10-04 03:25:16 +00:00
|
|
|
if (this.browsingContext.top == this.browsingContext) {
|
|
|
|
this.browsingContext.authorStyleDisabledDefault = true;
|
|
|
|
}
|
2023-11-24 09:10:52 +00:00
|
|
|
this.docShell.docViewer.authorStyleDisabled = true;
|
2018-05-25 17:22:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-10-24 20:51:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 01:34:15 +00:00
|
|
|
/**
|
|
|
|
* Returns links that would represent stylesheets once loaded.
|
|
|
|
*/
|
|
|
|
_collectLinks(document) {
|
|
|
|
let result = [];
|
|
|
|
for (let link of document.querySelectorAll("link")) {
|
|
|
|
if (link.namespaceURI !== "http://www.w3.org/1999/xhtml") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let isStyleSheet = Array.from(link.relList).some(
|
|
|
|
r => r.toLowerCase() == "stylesheet"
|
|
|
|
);
|
|
|
|
if (!isStyleSheet) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!link.href) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result.push(link);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:08:02 +00:00
|
|
|
/**
|
|
|
|
* Switch the stylesheet so that only the sheet with the given title is enabled.
|
|
|
|
*/
|
|
|
|
_switchStylesheet(title) {
|
2020-12-04 01:34:15 +00:00
|
|
|
let document = this.document;
|
|
|
|
let docStyleSheets = Array.from(document.styleSheets);
|
|
|
|
let links;
|
2019-10-27 18:08:02 +00:00
|
|
|
|
|
|
|
// Does this doc contain a stylesheet with this title?
|
|
|
|
// If not, it's a subframe's stylesheet that's being changed,
|
|
|
|
// so no need to disable stylesheets here.
|
2020-06-15 16:10:41 +00:00
|
|
|
let docContainsStyleSheet = !title;
|
|
|
|
if (title) {
|
2020-12-04 01:34:15 +00:00
|
|
|
links = this._collectLinks(document);
|
|
|
|
docContainsStyleSheet =
|
|
|
|
docStyleSheets.some(sheet => sheet.title == title) ||
|
|
|
|
links.some(link => link.title == title);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let sheet of docStyleSheets) {
|
|
|
|
if (sheet.title) {
|
|
|
|
if (docContainsStyleSheet) {
|
|
|
|
sheet.disabled = sheet.title !== title;
|
2020-06-15 16:10:41 +00:00
|
|
|
}
|
2020-12-04 01:34:15 +00:00
|
|
|
} else if (sheet.disabled) {
|
|
|
|
sheet.disabled = false;
|
2019-10-27 18:08:02 +00:00
|
|
|
}
|
2018-05-25 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 01:34:15 +00:00
|
|
|
// If there's no title, we just need to disable potentially-enabled
|
|
|
|
// stylesheets via document.styleSheets, so no need to deal with links
|
|
|
|
// there.
|
|
|
|
//
|
|
|
|
// We don't want to enable <link rel="stylesheet" disabled> without title
|
|
|
|
// that were not enabled before.
|
|
|
|
if (title) {
|
|
|
|
for (let link of links) {
|
|
|
|
if (link.title == title && link.disabled) {
|
|
|
|
link.disabled = false;
|
2019-10-27 18:08:02 +00:00
|
|
|
}
|
2018-05-25 17:22:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 02:54:17 +00:00
|
|
|
}
|
2018-05-25 17:22:14 +00:00
|
|
|
|
2023-02-24 21:09:36 +00:00
|
|
|
#collectAndSendSheets() {
|
|
|
|
let window = this.browsingContext.associatedWindow;
|
|
|
|
window.requestIdleCallback(() => {
|
|
|
|
if (!window || window.closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let filteredStyleSheets = this.#collectStyleSheets(window);
|
|
|
|
this.sendAsyncMessage("PageStyle:Add", {
|
|
|
|
filteredStyleSheets,
|
|
|
|
preferredStyleSheetSet: this.document.preferredStyleSheetSet,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-27 18:08:02 +00:00
|
|
|
/**
|
2020-11-30 21:37:15 +00:00
|
|
|
* Get the stylesheets that have a title (and thus can be switched) in this
|
|
|
|
* webpage.
|
|
|
|
*
|
|
|
|
* @param content The window object for the page.
|
2019-10-27 18:08:02 +00:00
|
|
|
*/
|
2023-02-24 21:09:36 +00:00
|
|
|
#collectStyleSheets(content) {
|
2018-05-25 17:22:14 +00:00
|
|
|
let result = [];
|
2020-12-04 01:34:15 +00:00
|
|
|
let document = content.document;
|
2018-05-25 17:22:14 +00:00
|
|
|
|
2020-12-04 01:34:15 +00:00
|
|
|
for (let sheet of document.styleSheets) {
|
|
|
|
let title = sheet.title;
|
|
|
|
if (!title) {
|
|
|
|
// Sheets without a title are not alternates.
|
2018-05-25 17:22:14 +00:00
|
|
|
continue;
|
2019-07-05 07:46:28 +00:00
|
|
|
}
|
2018-05-25 17:22:14 +00:00
|
|
|
|
|
|
|
// Skip any stylesheets that don't match the screen media type.
|
2020-11-30 21:37:15 +00:00
|
|
|
let media = sheet.media.mediaText;
|
|
|
|
if (media && !content.matchMedia(media).matches) {
|
2018-05-25 17:22:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-04 01:34:15 +00:00
|
|
|
// We skip links here, see below.
|
|
|
|
if (
|
|
|
|
sheet.href &&
|
|
|
|
sheet.ownerNode &&
|
|
|
|
sheet.ownerNode.nodeName.toLowerCase() == "link"
|
|
|
|
) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let disabled = sheet.disabled;
|
|
|
|
result.push({ title, disabled });
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is tricky, because we can't just rely on document.styleSheets, as
|
|
|
|
// `<link disabled>` makes the sheet don't appear there at all.
|
|
|
|
for (let link of this._collectLinks(document)) {
|
|
|
|
let title = link.title;
|
|
|
|
if (!title) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let media = link.media;
|
|
|
|
if (media && !content.matchMedia(media).matches) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let disabled =
|
|
|
|
link.disabled ||
|
|
|
|
!!link.sheet?.disabled ||
|
|
|
|
document.preferredStyleSheetSet != title;
|
|
|
|
result.push({ title, disabled });
|
2018-05-25 17:22:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-07-30 02:54:17 +00:00
|
|
|
}
|
|
|
|
}
|