Skip to content

Instantly share code, notes, and snippets.

@svenk
Created November 17, 2017 11:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save svenk/d932c8286c3b446c615a174f00fe71d5 to your computer and use it in GitHub Desktop.
Save svenk/d932c8286c3b446c615a174f00fe71d5 to your computer and use it in GitHub Desktop.
A Firefox Quantum ViewSourceWith extension workaround with Firefox's source.editor.external
# A Firefox Quantum ViewSourceWith replacement for Windows
#
# Usage:
# (1) Save as C:\path\to\script.ps1
# (2) In order to run your own powershell scripts, start PowerShell
# as Administrator and run the command
#
# Set-ExecutionPolicy RemoteSigned
#
# (2) You can test the proper running (from a user cmd):
#
# cd C:\path\to\
# # Create a "test.html" with some content here
# powershell.exe ".\test.ps1" -filename "test.html"
#
# (3) Determine the absolute path to powershell.exe from powershell itself
# via
#
# (Get-Command powershell).Path
#
# (4) In Firefox, edit about:config the following way:
#
# view_source.editor.external = true
# view_source.editor.path = "Path\to\powershell.exe"
# view_source.editor.args = "C:\path\to\script.ps1 -filename"
#
# (5) Click "View source with" in any Firefox tab. If it doesn't fire up
# Powershell, try the browser console to read what's the error.
# Firefox view_source.editor call arguments seems to be put together
# in https://searchcode.com/codesearch/view/18535188/ line 100
# also
# https://hg.mozilla.org/mozilla-central/file/0e91a598b99e/toolkit/components/viewsource/content/viewSourceUtils.js
# line 170
#
# (6) Include -noexit as argument to debug the script in case of errors.
#
#
# Written by SvenK on 2017-11-17 for Public Domain, intentionally for t29.
#
### my arguments were:
#### args = "C:\Users\Sven\Desktop\test.ps1" -noexit -filename
#### path = C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
# Problem so far: Firefox somehow not properly escapes the arguments and
# with powershell it is a pain to look into the reason for that.
#### -> will probably write a C++ file (mingw) instead.
param (
# Single parameter: A path to a HTML file
[Parameter(Mandatory=$true)][string]$filename
)
# show windows 10 toast notification
Function My-Show-Notification {
Param($Text)
$ErrorActionPreference = "Stop"
$notificationTitle = $Text
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
#Convert to .NET type for XML manipuration
$toastXml = [xml] $template.GetXml()
$toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
#Convert back to WinRT type
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "PowerShell"
$toast.Group = "PowerShell"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)
#$toast.SuppressPopup = $true
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
$notifier.Show($toast);
}
My-Show-Notification("called with filename=$filename")
# if you would slurp the HTML from a URL
# $URI="http://technikum29.de/de/"
# $Response = Invoke-WebRequest -Uri $URI
# $HTML = $Response.ParsedHtml
# instead, read the HTML file
$HTML = New-Object -Com "HTMLFile"
$HTML.IHtmlDocument2_write($(Get-content $filename -raw))
# from hereon, implement your own logic. Here, we look up the local
# filename from a meta tag such as
# <meta name="$meta_fieldname" content="C:\somewhere\over\the\rainbow.html">
# the meta tag name where to get the information from
$meta_fieldname = "author"
# the editor to start
$editor = 'C:\Program Files (x86)\Notepad++\notepad++.exe'
# query content of the specific meta tag
$new_filename = ($HTML.getElementsByTagName('meta') | where {$_.name -eq $meta_fieldname }).content
# check if that file exists
if([string]::IsNullOrEmpty($new_filename)) {
My-Show-Notification("Sourcecode is not one of our websites, opening raw HTML.")
$new_filename = $filename
} elseif(!([System.IO.File]::Exists($new_filename))) {
My-Show-Notification("Determined file location '$new_filename' does not exist, opening raw HTML instead")
$new_filename = $filename
} else {
My-Show-Notification("Found file location at $new_filename")
}
# Safely escape the command arguments:
#$command = "& "editor --%" + $new_filename
# does not work. Do the stupid instead:
& $editor "$new_filename"
@svenk
Copy link
Author

svenk commented Nov 17, 2017

Note the status: Doesn't work for me on Windows currently.

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