remove css from html files

Post Reply
janwolk78
Newbie
Posts: 1
Joined: Wed Jan 20, 2016 3:20 pm

remove css from html files

Post by janwolk78 »

Dear all, please take a look at the code below. if i use it within Switch, i get the error that the variable document is not known. As i have absolutely no idea from coding, i would highly appreciate any hints! :-)

Code: Select all

// Is invoked each time a new job arrives in one of the input folders for the flow element.
// The newly arrived job is passed as the second parameter.
function jobArrived( s : Switch, job : Job )
{

/*
  Remove inline style="..."
  Preserve hidden content.
  Call the function like this:
    var all = document.getElementsByTagName('*');
    remove_style(all);
  Note: Selecting all elements in the page via a
  wildcard query could be slow, depending on how
  many elements are in the page. You could use a
  smaller set of elements to be more performant:
    var set = document.getElementById('foo').getElementsByTagName('bar');
    remove_style(set);
*/

function remove_style(list) {
  'use strict';

  // Presentational attributes.
  var attr = [
    'align',
    'background',
    'bgcolor',
    'border',
    'cellpadding',
    'cellspacing',
    'color',
    'face',
    'height',
    'hspace',
    'marginheight',
    'marginwidth',
    'noshade',
    'nowrap',
    'valign',
    'vspace',
    'width',
    'vlink',
    'alink',
    'text',
    'link',
    'frame',
    'frameborder',
    'clear',
    'scrolling',
    'style'
  ];

  // Used later.
  var attr_len = attr.length;
  var i = list.length;

  // Defined in loop.
  var j;
  var is_hidden;

  // Loop through node list.
  while (i--) {
    is_hidden = list[i].style.display === 'none';

    // Decrement `j` in next loop.
    j = attr_len;

    // Loop through attribute list.
    while (j--) {
      list[i].removeAttribute(attr[j]);
    }

    /*
      Re-hide display:none elements,
      so they can be toggled via JS.
    */
    if (is_hidden) {
      list[i].style.display = 'none';
    }
  }
     var all = document.getElementsByTagName('*');
    remove_style(all);
}

}
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: remove css from html files

Post by gabrielp »

Switch Scripter doesn't allow you to reference a document object. Switch has a limited JS implementation. I haven't really messed with HTML in Switch Scripter but to do this I imagine you'd have to make some sort of html parser.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
bens
Advanced member
Posts: 253
Joined: Thu Mar 03, 2011 10:13 am

Re: remove css from html files

Post by bens »

JavaScript code written for web pages can usually not be reused verbatim in Switch because the environment is very different. One is meant to be executed in the context of a browser, the other in a workflow automation tool. It's not unlike trying to use an igloo in the Sahara.

The code you posted removes (some) CSS from an HTML document when that document is loaded in a browser. It does this entirely in the browser's memory - that is, the actual HTML file on disk is never changed.

On the other hand, Switch is all about files on disk. Switch moves files around, manipulates metadata, and invokes other applications - everything based on files on disk.

So there is a discrepancy between the intention of the script (editing a browser's internal representation of a document) and Switch (manipulating files on disk). I would suggest you go back to the beginning: think about *why* you want that piece of code, and see if there is a different solution.

If you really want that code to be used on a particular HTML document, then you will need to inject it into the file as is, save that HTML file to disk, and then let a browser do its thing as normal. Depending on the complexity of the HTML file this can be relatively simple or quite complex, and I *strongly* suggest you get someone with at least some coding experience to do it.
Post Reply