scriptscat / scriptcat

脚本猫,一个可以执行用户脚本的浏览器扩展
https://docs.scriptcat.org
GNU General Public License v3.0
1.7k stars 137 forks source link

New metablock @filetype #211

Closed sjehuda closed 9 months ago

sjehuda commented 1 year ago

I think it would be most appropriate to incorporate the capability of the program Open in Browser into metablocks as follows:

// @filetype text/xml text/html
// @filetype application/xml text/html
// @filetype application/rss+xml text/html
// @filetype application/atom+xml text/html

@filetype: @mimetype would be good too text/html: text/plain would be good too

About

This is a request to add new API to GM.xmlHttpRequest, or to the core of the Greasemonkey extension, that can influence a change in document.contentType.

Preface

I have wrote a userscript that converts structured data files (i.e. XML and JSON) into human readable HTML files.

Overhaul, the program works as expected; documents are processed and forged successfully into HTML.

All the extra functionalities work well when document.contentType (read only) does not contain xml.

However, when document.contentType has xml, any code that contains querySelector would fail because the document is believed to be whatever document.contentType has determined upon initial request (an XML file), and, apparently, the web browser doesn't check document once it has been loaded.

An error message from the program suggests the file processed is XML when in reality it is an HTML, hence hinders other functions to be executed:

Expected Behavior

Execute actions on document as as HTML.

Actual Behavior

Browser blocks HTML actions (throws errors) because it is believed to be an XML.

Script

https://greasyfork.org/en/scripts/465932-newspaper-native-rss-reader

Test Page

https://lzone.de/liferea/blog/feed.xml application/xml

Note

More information at https://github.com/greasemonkey/greasemonkey/issues/3164

CodFrm commented 1 year ago

This is an interesting script, I looked at Tampermonkey and Violentmonkey, it seems to be working, but Scriptcat is not working, let me first solve this problem 😂.

In addition, can we solve this problem from other aspects? For example, Web Component, the style of this attribute will inherit from custom-card, but this seems troublesome

image
sjehuda commented 1 year ago

This is an interesting script

Thank you.

I looked at Tampermonkey and Violentmonkey, it seems to be working, but Scriptcat is not working, let me first solve this problem 😂.

It doesn't work with Greasemonkey neither. I think it stopped to work with Greasemonkey after I've added more GM.getValue and GM.setValue.

In addition, can we solve this problem from other aspects? For example, Web Component, the style of this attribute will inherit from custom-card, but this seems troublesome

I'm an Attorney at Law, not a programmer.

Please kindly explain what do you mean by "Web Component".

As I stated, I think that a metablock (@filetype or @mimetype) that would imitate "Open in Browser" should handle this task, not only for XML files, but also images archives and documents.

See my comments at https://github.com/violentmonkey/violentmonkey/issues/1842#issuecomment-1613353143

sjehuda commented 1 year ago

Please navigate to about:config Set devtools.jsonview.enabled to false

And test this script with these pages:

https://www.jsonfeed.org/feed.json

https://nu.federati.net/api/statuses/user_timeline/2.as

http://scripting.com/rss.json

https://peertube.su/feeds/videos.json?sort=-trending

https://librivox.org/feed/

Video preview of the script in action in a page that document.contentType doesn't contain xml.

https://github.com/violentmonkey/violentmonkey/assets/19249089/e8c2770a-5ebd-4c7b-b42f-0bb3639565e5

CodFrm commented 1 year ago

This is the document of Web Component: https://developer.mozilla.org/en-US/docs/Web/API/Web_components, my idea is to use its shadow DOM feature to change the outer custom-card style Inheritance, but I'm not sure if I can solve this problem, and it's very troublesome.

What's weird is that some elements have element.style and some don't.

In short, see if there is any other solution to this problem. If not, I will consider trying to add the metablock @filetype you mentioned (but maybe in this form: https://github.com/scriptscat/scriptcat/issues/54)

sjehuda commented 7 months ago

I have managed to overcome this setback, namely by passing the HTML element newDocument and processing it in all ways feasible (see preProcess) and then replacing document by newDocument. See v24.04.06 vs. v24.04.08.

There was also another matter which required to create an element and change CSS Stylesheet in order to get the expected behaviour, because JavaScript attribute style does not work with document.contentType that ends with XML. See v24.04.08 vs. v24.04.09.

The only functionality which remains not to work upon XML is mode switcher (bright and dark) due to attribute style, yet it might be possible to fix.