Using both srcdoc and src when rendering p5js script - iframe

I'd like to control the uniqueness of the output of a script "externally", i.e. through URL params or similar to supply a seed for randomSeed.
I load my p5js script in an iframe through srcdoc, so first I prepare it by wrapping it in some HTML, then I set the whole string as the srcdoc.
I've tried then adding a src attribute as well as the srcdoc - hoping to be able to use p5js native function getURLParams();
Although - regardless that src is set to something like = https://somelink.com?seed=500, getURLParams() returns {}. Further, getUrl returns about:srcdoc.
So, then I've tried sticking everything in src by base64:ing the document, like so base64src = 'data:text/html;base64,' + btoa(script) + '?seed=500'; I've also tried without base64 and just passing in data:text/html, + script.
But, this never ends up rendering. What am I missing?

Related

How to get rid of the original file path instead of name in PGP encryption with ChoPGP library?

I tried PGP encrypting a file in ChoPGP library. At the end of the process it shows embedded file name along with the whole original file name path.
But I thought it will show only the filename without the whole path. Which I intend to work on and figure out a way to do so?
Doing the following:
using (ChoPGPEncryptDecrypt pgp = new ChoPGPEncryptDecrypt())
{
pgp.EncryptFile(#"\\appdevtest\c$\appstest\Transporter\test\Test.txt",
#"\\appdevtest\c$\appstest\Transporter\test\OSCTestFile_ChoPGP.gpg",
#"\\appdevtest\c$\appstest\Transporter\pgpKey\PublicKey\atorres\atorres_publicKey.asc",
true,
true);
}
which will result in:
But I would like to only extract the Test.txt in the end something
like this:
Looking at this line from the ChoPGP sources: https://github.com/Cinchoo/ChoPGP/blob/7152c7385022823013324408e84cb7e25d33c3e7/ChoPGP/ChoPGPEncryptDecrypt.cs#L221
You may find out that it uses internal function GetFileName, which ends up with this for the FileStream: return ((FileStream)stream).Name;.
And this one, according to documentation, Gets the absolute path of the file opened in the FileStream..
So you should either make fork of ChoPGP and modify this line to extract just filename, or submit a pull request to the ChoPGP. Btw, it's just a wrapper around the BouncyCastle so you may use that instead.

Get Next and Previous Tags (AwesomeWM)

Is there a way to get the next or previous tag in AwesomeWM Lua Config?
Reason :- I want to get the next or previous tag to set the focused clients tag to it and then move to the tag.
I know there is awful.tag.viewnext which moves to the next tag but unsure about how to get the tag it will move to to set this on a client.
Thanks in advance
Well... yes, there is a way, but there is not a completely trivial one.
First, let's look at the default config. What existing functionality has to get next/previous tags? That is the mouse wheel on the taglist:
https://github.com/awesomeWM/awesome/blob/7a759432d3100ff6870e0b2b427e3352bf17c7cc/awesomerc.lua#L160-L161
So, how does awful.tag.viewnext and viewprev get the next tag? These functions just call viewidx with an idx of 1 or -1, e.g.:
https://github.com/awesomeWM/awesome/blob/7a759432d3100ff6870e0b2b427e3352bf17c7cc/lib/awful/tag.lua#L1494
So, awful.tag.viewidx can do what we want. How does it do it?
It gets a table of all un-hidden tags, finds the index of the currently selected tag and then uses gears.math.cycle to compute the index of the tag with the requested offset.
https://github.com/awesomeWM/awesome/blob/7a759432d3100ff6870e0b2b427e3352bf17c7cc/lib/awful/tag.lua#L1452-L1469
For you, something like the following should do the trick:
function get_tag_at_offset(i, s)
s = screen[s or awful.screen.focused()]
local tags = s.tags
local showntags = {}
for _, t in pairs(tags) do
if not awful.tag.getproperty(t, "hide") then
table.insert(showntags, t)
end
end
for k, t in ipairs(showntags) do
if t == sel then
return showntags[gears.math.cycle(#showntags, k + i)]
end
end
end
The above function can be used like get_tag_at_offset(1) to get the next tag and with argument -1 for the previous. Where possible, I would recommend to also pass in a screen via the second argument.
Also, all of this is completely untested and just written on the spot. There could very well be typos and other mistakes in here.

Randomizing URL numbers in iMacros

I'm using iMacros because I want to scrape a certain site for ID's which are used in the URL, after which I want to press a button.
I know you can't use Regular Expressions or globbing in the syntax for URL GOTO.
But I figured there might be a way to enter variables into the URL GOTO=?
Preferable I wouldn't want to randomize the variable, but have it try every page from [1 - 99999]
This is what I currently have:
VERSION BUILD=8940826 RECORDER=FX
TAB T=1
SET !ERRORIGNORE YES
SET !VAR3 ("Math.floor(Math.random()*99999 + 1);")
URL GOTO=http://example.com/id/ "randomized_variable_here"
TAG POS=1 TYPE=SPAN ATTR=TXT:press<SP>button
I have tried a few things, but I don't seem to be able to do this.
I have very little experience actually creating stuff for myself, I just modify scripts to fit my purposes, but should I look towards an HTML document or something like that to randomize that variable for me?
Thanks in advance!
It's pretty simple to get the string with a randomized variable:
' ...
SET !VAR3 EVAL("Math.floor(Math.random()*99999 + 1);")
URL GOTO=http://example.com/id/{{!VAR3}}
' ...
And the following code is for looping through [1 - 'Max:' value on the 'iMacros' sidebar]:
' ...
SET !LOOP 1
URL GOTO=http://example.com/id/{{!LOOP}}
' ...
Just play this macro in loop mode.

How Can WYSIHTML5 output inline CSS?

I am running WYSIHTML5 to allow myself to enter email text and format it for sending as HTML. However when I view HTML of the formatted text I get classes associated with elements for Colors. This is expected behavior but since I need to send the output in an email hence I would like to have those colors to be in Inline CSS, since I cannot attach CSS files with the email like that. Example here
<span class="wysiwyg-color-green">Testing</span>
That is if I select green color for text: Testing. Is there any way to modify that green to become part of html itself like
<span style="color:green">Testing</span>
I have tried to search for this but could not find, so I am not asking without first looking for it. If anybody could please just point somewhere. Even a link to any guide to this, will do. I do not wish that you spend time writing code for me.
You could do it with php :
str_replace ( 'class="wysiwyg-color-green"', 'style="color:green"' ,$html)
You can do the same with javascript, altrough it's always safer to do everything server-side.
http://www.w3schools.com/jsref/jsref_replace.asp
Here's the javascript code I used but may be a good idea to heed Jean-Georges warning above:
replaceColorStylesWithInlineCss = function (htmlContents){
result = htmlContents.replace('class="wysiwyg-color-black"', 'class="wysiwyg-color-black" style="color:black"');
result = result.replace('class="wysiwyg-color-silver"', 'class="wysiwyg-color-silver" style="color:silver"');
result = result.replace('class="wysiwyg-color-gray"', 'class="wysiwyg-color-gray" style="color:gray"');
result = result.replace('class="wysiwyg-color-maroon"', 'class="wysiwyg-color-maroon" style="color:maroon"');
result = result.replace('class="wysiwyg-color-red"', 'class="wysiwyg-color-red" style="color:red"');
result = result.replace('class="wysiwyg-color-purple"', 'class="wysiwyg-color-purple" style="color:purple"');
result = result.replace('class="wysiwyg-color-green"', 'class="wysiwyg-color-green" style="color:green"');
result = result.replace('class="wysiwyg-color-olive"', 'class="wysiwyg-color-olive" style="color:olive"');
result = result.replace('class="wysiwyg-color-navy"', 'class="wysiwyg-color-navy" style="color:navy"');
result = result.replace('class="wysiwyg-color-blue"', 'class="wysiwyg-color-blue" style="color:blue"');
result = result.replace('class="wysiwyg-color-orange"', 'class="wysiwyg-color-orange" style="color:orange"');
return result
};
Note: I kept the wysiwyg styles in there because I'm saving to the db and want it to display properly in the wysihtml5 section when I load it again. DRY it up if you're clever.

Is it possible to parse a stylesheet with Nokogiri?

I've spent my requisite two hours Googling this, and I can not find any good answers, so let's see if humans can beat Google computers.
I want to parse a stylesheet in Ruby so that I can apply those styles to elements in my document (to make the styles inlined). So, I want to take something like
<style>
.mystyle {
color:white;
}
</style>
And be able to extract it into a Nokogiri object of some sort.
The Nokogiri class "CSS::Parser" (http://nokogiri.rubyforge.org/nokogiri/Nokogiri/CSS/Parser.html) certainly has a promising name, but I can't find any documentation on what it is or how it works, so I have no idea if it can do what I'm after here.
My end goal is to be able to write code something like:
a_web_page = Nokogiri::HTML(html_page_as_string)
parsed_styles = Nokogiri::CSS.parse(html_page_as_string)
parsed_styles.each do |style|
existing_inlined_style = a_web_page.css(style.declaration) || ''
a_web_page.css(style.declaration)['css'] = existing_inlined_style + style.definition
end
Which would extract styles from a stylesheet and add them all as inlined styles to my document.
Nokogiri can't parse CSS stylesheets.
The CSS::Parser that you came across parses CSS expressions. It is used whenever you traverse a HTML tree by CSS selectors rather than XPath (this is a cool feature of Nokogiri).
There is a Ruby CSS parser, though. You can use it together with Nokogiri to achieve what you want.
require "nokogiri"
require "css_parser"
html = Nokogiri::HTML(html_string)
css = CssParser::Parser.new
css.add_block!(css_string)
css.each_selector do |selector, declarations, specificity|
html.css(selector).each do |element|
style = element.attributes["style"]&.value || ""
element.set_attribute('style', [style, declarations].compact.join(" "))
end
end
#molf definitely had a great start there, but it still required debugging a handful of problems to get it working in production. Here is the current, tested version of this:
html = Nokogiri::HTML(html_string)
css = CssParser::Parser.new
css.add_block!(html_string) # Warning: This line modifies the string passed into it. In potentially bad ways. Make sure the string has been duped and stored elsewhere before passing this.
css.each_selector do |selector, declarations, specificity|
next unless selector =~ /^[\d\w\s\#\.\-]*$/ # Some of the selectors given by css_parser aren't actually selectors.
begin
elements = html.css(selector)
elements.each do |match|
match["style"] = [match["style"], declarations].compact.join(" ")
end
rescue
logger.info("Couldn't parse selector '#{selector}'")
end
end
html_with_inline_styles = html.to_s

Resources