Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Firemonkey] New metablock @filetype #568

Closed
sjehuda opened this issue Jun 30, 2023 · 15 comments
Closed

[Firemonkey] New metablock @filetype #568

sjehuda opened this issue Jun 30, 2023 · 15 comments

Comments

@sjehuda
Copy link

sjehuda commented Jun 30, 2023

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 the core of the Firemonkey 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 scriptscat/scriptcat#211

@erosman
Copy link
Owner

erosman commented Jun 30, 2023

However, when document.contentType has xml, any code that contains querySelector would fail

querySelector works on XML.

Here is a test script:

// ==UserScript==
// @name          New metablock @filetype #568
// @match         https://lzone.de/liferea/blog/feed.xml
// @version       1.0
// ==/UserScript==


console.log(document.querySelector('subtitle'));

Scope of UserScripts

As also mentioned in violentmonkey/violentmonkey#1842 (comment) , I also believe that the scope of userscripts is, and should be, limited to the webpage, as originally intended.

GM functions were added to facilitate some of the common difficulties. HTTP headers are in the scope of the browser. TM provides some functionality beyond the scope of the webpage (e.g. `GM_webRequest, or GM_cookie.list, but that is not supported by other managers.

Allowing userscripts that are meant to be page scripts, access to browser level API can pose a security risk, as well as interfere with functionality of the browser and/or other extensions.

@sjehuda
Copy link
Author

sjehuda commented Jul 2, 2023

document.contentType

querySelector works on XML.

Then something else isn't working the same as it would, if the document.contentType was text/plain.

This is the problem violentmonkey/violentmonkey#1842 (comment)

Even if by setting content-type would be possible arbitrarily, and not being bound to the content-type selections offered by the server, then I would still have this problem.

The following code, won't solve the issue. Tampermonkey/tampermonkey#1809 (comment)

    GM.xmlHttpRequest({
      method: 'GET',
      url: documentURI,
      headers: {
        "Content-Type": "text/plain",
        "Accept": "text/plain"
      },
      onprogress: function(request) {
        request.responseType = 'text';
      },
      onload: function(request) {
        request.overrideMimeType = 'text/plain';
        if (document.URL.startsWith('file:') ||
            request.status == 200) {
          myResolve(request);
        }
        else {
          myReject("File not Found");
        }
      },
      onerror: function(request) {
        myReject('File not Found')
      }
    })

Vendor bullying

Another reason to add the proposed metablock is to overcome bullying from web browser vendors who decide upon technologies they want to censor.

This userscript was born because of the attempt of the red lizard and globe fox to conceal RSS.

They have done it by:

  • removing the rss icon to bookmarks (2010)
  • removing rss completely (2018)
  • forcing download and not allowing to view files of type application/atom+xml application/rss+xml files.
  • unless they are text/xml or application/xml (2018)

  • yet they do display json files in a "professional manner"

It makes no sense.
Why did they decide this way?

This is censorship of independent media and people under the guise of UI improvements.
This can not be misconstrue.

JavaScript scope

However, due to the fact that we can process any file in Javascript, including documents (ODT and PDF), archives (7Zip and Gzip) and even databases, I suggest to add the @filetype / @MimeType metablocks.

@erosman
Copy link
Owner

erosman commented Jul 2, 2023

In order to investigate further, please provide a minimal userscript that demonstrate the issue.

@sjehuda
Copy link
Author

sjehuda commented Jul 2, 2023

Because, technically, an example userscript won't be of use, the following example should be enough.

If you insist, I will provide you a minimal script.

Suppose I want to turn data into a table or I want to process a web feed, as I do with my script.

Visit this page with Firefox https://reclaimthenet.org/feed
You won't be able to process that xml file, because Firefox will force (prompt) you to download that file instead.

@sjehuda
Copy link
Author

sjehuda commented Jul 5, 2023 via email

@erosman
Copy link
Owner

erosman commented Jul 5, 2023

Here is a simple script that doesn't appear to work with xml, because some properties are exclusive to HTML and XML.

Can you give an example site to test?

The script is meant to run only when tab is showing an image.
if (!document.contentType.startsWith('image/')) { return; };

@sjehuda
Copy link
Author

sjehuda commented Jul 5, 2023 via email

@sjehuda
Copy link
Author

sjehuda commented Jul 5, 2023 via email

@erosman
Copy link
Owner

erosman commented Jul 5, 2023

Here is a simple script, tested on Firefox.

// ==UserScript== 
// @name          Falkon Image Fix
// @namespace     i2p.schimon.falkon.image
// @description   Script description
// @match         *://*/*
// @version       1.0.0
// ==/UserScript==

// IIF anonymous function wrapper, for error checking & limiting scope
(() => {
  if (!document.contentType.startsWith('image/')) { return; }

  // get the image
  const xml = document.contentType === 'image/svg+xml';
  const img =  xml ? document.documentElement : document.querySelector('img');
  if (!img) { return; }

  // limit the max size
  img.style.maxHeight = '100vh';
  img.style.maxWidth = '100vw';

  // make some display changes
  document.title += ' ' + document.contentType;
})();

@sjehuda
Copy link
Author

sjehuda commented Jul 6, 2023

Yes. It works with Falkon too, both html (png etc.) and xml (svg).

I still insist that there are things that are specific to HTML which can not be done with HTML. See also violentmonkey/violentmonkey#1842 (comment)

Yet, in any case, I will address this:

You won't be able to process that xml file, because Firefox will force (prompt) you to download that file instead.

The problem is really the prompt which doesn't allow to open XML files of Atom, RSS and perhaps other formats like ODT and ODS which can be processed using Javascript.

So I'm still in favor of metablock @filetype.

@erosman
Copy link
Owner

erosman commented Jul 6, 2023

The problem is really the prompt which doesn't allow to open XML files of Atom, RSS and perhaps other formats like ODT and ODS which can be processed using Javascript

I would fetch the file and process it.

@sjehuda
Copy link
Author

sjehuda commented Jul 7, 2023

I didn't think of doing so.

I'll look into it.

@sjehuda
Copy link
Author

sjehuda commented Jul 7, 2023

Some feeds are located on different servers so I would likely need to use GM.xmlHttpRequest.

First observation

If script is executed from an HTML page, then I can fetch the feed and replace the current page with a processed feed.

Problem 1

Upon pressing button Back, user skips the current page, instead of getting back to that page.

Solution 1

Pseudo back button Insert current page to history.

Problem 2

The current page might have Javascript running, and these running scripts can not be turned off.

Conclusion

Rejected.

Second observation

Use GM_openInTab to open feed in new tab.

Problem

That may be annoying.

Third observation

GM_openInTab might not be needed. See bookmarklets at https://www.squarefree.com/bookmarklets/pagelinks.html

@erosman
Copy link
Owner

erosman commented Jul 7, 2023

Allowing userscripts that are meant to be page scripts, access to browser level API can pose a security risk, as well as interfere with functionality of the browser and/or other extensions.

In any case, as mentioned earlier, changing headers is beyond the scope of userscripts.

@erosman erosman closed this as completed Jul 27, 2023
@sjehuda
Copy link
Author

sjehuda commented Apr 8, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants