Vim: Make chrome refresh anytime I :write - google-chrome

I want a solution that does what's described in the vim wiki here, but that works on Chrome.
That is, I'm trying to avoid this:
Edit your HTML/CSS file.
Hit save in Vim.
CMD/CNTRL/ALT + TAB over to Firefox.
Press Ctrl-R in Firefox to refresh.
CMD/CNTRL/ALT + TAB back to Vim.
Do it again and again, wincing a little bit each time.
The vim wiki solution is for firefox, and other scripts and solutions I've found on the web are Mac only. But I'm on windows, and will often have vim open on the left half of the screen (editing html) and chrome open on the right half of the screen (displaying the file I'm editing).
A really "dumb" solution would work fine for me. That is, there wouldn't even need to be communication of the filename between vim and the browser. If I could just turn on a mode in vim, call it "auto-refresh", and now anytime I do a :w the currently active tab in chrome refreshes itself, without taking focus off the vim window. That would be perfect. Is it possible?

I don't know how to reload a give chrome page from shell, however, I agree with Chiel92 that if you need to see your changes when file changes, you can do that from browser.
See LiveReload, works with Windows & Mac (not for me then) and supports Safari & Chrome.
LiveReload will check your main page as well all css and javscript that it depends from, if any of those changes, it will reload it.
They seems to have launched a second version, however official site of version 2 it's offline and doesn't seem version 2 it's on github either. (Version 1 it's it's on github)

a solution that might work for you is in your html coding include the line in the head tag
<meta http-equiv="refresh" content="30" />
this will reload the page every 30 seconds directly taking from
w3schools.com
now when you want to deploy it just remove that line

If you are working on mac, then a bit of apple script does the job.
putting this function in your vim configuration will let you automate the process of switching between windows and refreshing you described.
function! ReloadBrowser()
silent exe "!osascript -e 'tell app \"Firefox\" to activate\<cr>
\tell app \"System events\"\<cr> keystroke \"r\" using command down\<cr>
\end tell'"
silent exe "!osascript -e 'tell app \"Iterm2\" to activate'"
endfunction
Calling that function will change system focus to Firefox, hit CMD-R to refresh the page, then change focus back to Iterm2.
Change 'Firefox' and 'Iterm2' to fit your workflow.
You can type the function into the vim command prompt like :call ReloadBrowser() or trigger the call with a mapping like this:
nnoremap <leader>rl call ReloadBrowser()<cr>
To trigger the call any time you write the file you could use an autocommand.
augroup AutoReload
au!
autocmd BufWritePost *.filetype call ReloadBrowser()
augroup END
That could get a bit annoying though so if you really want that behavior I think it would be best to make it toggle-able like this:
let s:auto_reload = v:false
function! ToggleAutoReload()
if s:auto_reload
augroup AutoReload
au!
autocmd BufWritePost *.filetype call ReloadBrowser()
augroup END
else
augroup AutoReload
au!
augroup END
endif
let s:auto_reload = !s:auto_reload
endfunction
with that in place you can either manually trigger the reload with <leader>rl or use :call ToggleAutoReload() to enable automatic reloading when you save the file, and a second :call ToggleAutoReload() will disable it when you're done.

I've tried LiveReload in the past but it wasn't a very dependable solution: it was very buggy and unstable and the installation process was too involved for too little effect.
Since then, (more than a year) I've been using a small Chrome extension that automatically reloads the webpage every x seconds. It's not "smart" at all but it works well both for local files and hosted files. I've used it countless times without any side effects whatsoever.
That Solution on the Vim Wiki is possible because of MozREPL which allows an external process to interact with Firefox. From there it's quite trivial to write an autocmd that triggers a refresh on :w or on CursorHold. But AFAIK there's no such tool for Chrome/Chromium and they don't offer an external API. LiveReload is a brilliant but failed hack and I believe that you'll have to settle with the dumb solution if you must work with Chrome.
edit
I just remembered a script that works very well on this Linux box but is a little bit limited on Mac OS X and doesn't seem to work on Windows. Essentially you register a window/tab with a part of it's name:
$ webrf setup-by-search test.html
then you simply do:
$ webrf refresh
to refresh the page.

I use WSL and two screens so for me this works. (as with every :write is a bit too much)
nmap <leader>t :silent !powershell.exe -command "Add-Type -AssemblyName System.Windows.Forms; [Windows.Forms.Cursor]::Position = \"-1290,50\"; Add-Type -MemberDefinition '[DllImport(\"user32.dll\")] public static extern void mouse_event(int flags, int dx, int dy, int cButtons, int info);' -Name U32 -Namespace W; [W.U32]::mouse_event(6,0,0,0,0); [Windows.Forms.Cursor]::Position = \"40,40\"; [W.U32]::mouse_event(6,0,0,0,0);"<CR><CR>
It basicly tells vim through a powershell command to have the mouse cursor go to:
position -1290, 50 ( which is where the refresh button of chrome is)
left mouse click
position 40,40 (which is where my vim is in full screen)
left mouse click
Step 3 and 4 is needed to refocus vim I tried to sent ALT-TAB but my vimrc disliked the % symbol.

On Windows OS you can utilize an AutoHotKey script that will automate that for a key press.
For example, the following script will bind Left Alt + r to switch to the browser window, send a Control + r(Refresh) and then get back to your previous window.
Replace Title_of_your_webpage with your browser's window title string after your page has loaded in it.
!r::
WinGet, winid, , Title_of_your_webpage
WinActivate, ahk_id %winid%
Send ^r
Send !{Tab}
Return

Related

How to save breakpoints in selenium ide chrome?

Is there a way to save breakpoints in Selenium IDE (Chrome)? I have a test which fills up one form, submits it and fills another form on another page etc. I want to pause this flow with breakpoints, maybe change some fields etc. If I save the project (.side), it won't save the breakpoints. It's very tedious to add them every time I start the tests.
How to solve this?
SIDE version 3.17.0
Chrome Mac version 81.0.4044.138 (Official Build) (64-bit)
You can use command to save it ( "command": "debugger").
The issue is that you don't have a command that can enable / disable those debugger commands like the (Ctrl + y). :(
But once your debugger commands are saved then any text editor can switch them on/off by replacing ["command": "debugger"] , with ["command": "//debugger"]

WebStorm/PhpStorm, issue with terminal on Windows 10

I decided to try Windows 10, but having problem to make terminal works inside WebStorm/PhpStorm.
When I open terminal, sometimes it's completely black, sometimes it loads the project folder but I cannot type there. There is a way to run it in the external window, but I just got accustomed to work with console like internal window inside WebStorm/PhpStorm...
I tried cmd, Power Shell - same results.
Any ideas?
Workaround: Terminal works fine in Windows 10 if you use legacy console.
To do so, open a command prompt window, right click on the title, then select Properties. At the bottom, check "Use legacy console". Confirm the dialog window and go back to WebStorm to launch a new terminal. It should work.
In any case, this is the ticket to watch after:
https://youtrack.jetbrains.com/issue/IDEA-143300 -- star/vote/comment to get notified on progress.
UPDATE:
This issue has now been resolved -- the fix will arrive in next major version -- PhpStorm v10/WebStorm v11 (or whatever that version will be as long as it's based on 142.xx branch or newer).
UPDATE:
For those who have problems with right clicking and finding "Properties" menu entry:
Just for the record: I have confirmation from JetBrains that it will be fixed (i.e. work with non-legacy mode) in WebStorm 11 which is scheduled for the beginning of November. In the EAP version it should already work.

Sublime Text 2 -> Vim, How do I open html file in Chrome?

Just getting started with Vim and am writing a basic HTML file. In SublimeText you could just right click and open in browser, but i'm having trouble finding how to do the same with Vim. I have Ubuntu and I want to preview the code in Chrome.
Thanks!
You can use :! to execute external commands within vim (see :help :!). The % sign in an :ex command expands to the current file name (see :help :_%). That being said, you can use the following to first save the file and then open it in the browser of your choice
:w|!google-chrome %
If you'd rather use firefox or chromium, use firefox or chromium, respectively, in place of google-chrome. If you're on mac or linux you could also use the open or xdg-open commands, respectively, to open the file in its default application. Don't want to type that out every time? Make your own command or mapping in your .vimrc.
command! ViewInBrowser :w|!google-chrome %
nnoremap <leader>b :ViewInBrowser<cr>

Does chrome dev tools have anything similar to sublime text's snippets and tab triggers?

When I'm writing code in sublime text, I have the ability to create "snippets" that I can "tabtrigger" into my code.
For example, If I'm writing some js code and I want to log something to the console, I created a snippet so that all I have to do is type "log" and hit tab. As soon as I hit tab console.log(); is inserted into my code. Google seems to have a different definition of snippets, and I was wondering if the described functionality is available when writing code directly in the dev tools source panel?
I did not find this Tab trigger ability in Chrome but a way to simmulate this operation:
Using AutoHotKey
Run it after installing , and right click AutoHotKey in the taskbar corner -> Edit This Script
Add the following to the script files.
#IfWinActive Developer Tools -
::ml::
SendInput {Raw}margin-left:10px;
return
write your own script like above, then save it and Reload This Script
Have fun!
More to say:
#IfWinActive means this part only applies to the current window whose name started with Developer Tools - .So if you want this script work, you shoul make the develop window seperated from the main Chrome window to have this window name.
::ml:: defines the keys to trigger, the starting :: means this is a word , which can only trigger when it is followed by a space key or an enter key or a tab key a semicolon key and so on. Otherwise it will soon output margin-left:10px; once you typed ml .
SendInput with {Raw} can avoid triggerring endless loop. eg. bb -> border-bottom:1px solid #bbb; the output is ended with bb; and AutoHotKey will auto minimize the current window to avoid endless loop.

Disable shortcuts in Google Chrome

Is there a way to disable and replace shortcut commands in Google Chrome. I want to use Chrome for a public computer that only can access one site. Because of this I want to disable keys like Ctrl+Tab, Ctrl+T, Alt+F4 and I want to change F11 to a command like Ctrl+Shift+Alt+J (example) to stop users from exiting full screen mode.
Settings on the network block everything but a specific domain but now I want to block the user from exiting the browser.
I know this question is quite old, but I found a solution that works for me (and probably many others too).
If you don't use the CTRL and ALT keys in the browser at all, you can disable them in the OS itself.
Under Linux, I used xmodmap -pke to find out which key is mapped to which code.
Knowing the keycodes, I used:
xmodmap -e "keycode 37 = "
xmodmap -e "keycode 105 = "
to disable both left and right CTRL keys (to prevent something like CTRL+W, CTRL+T, etc.), then with:
xmodmap -e "keycode 133 = "
xmodmap -e "keycode 134 = "
both left and right SUPER keys (Windows Keys) (to prevent opening the start menu and such fancy stuff)
and then finally with xmodmap -e "keycode 105 = " I disabled the ALT key (to prevent ALT+F4, etc.).
And optionally, we can disable F1 too, so that the Chrome Support Page doesn't open, with: xmodmap -e "keycode 67 = "
Finally, let Chromium or Chrome lock the rest down for us using Kiosk Mode:
chromium-browser --kiosk http://example.com/
or
google-chrome --kiosk http://example.com/
And right click is already disabled in kiosk mode, so we don't need to change anything there.
With all that done, the end user can only navigate with the mouse within the predefined webpage (And links leading to some other content, of course) and write stuff with the normal characters on the keyboard, but nothing more.
Reloading may be still possible (F5), but even that can be disabled with: xmodmap -e "keycode 71 = "
Caution: Please execute xmodmap -pke first to discover if your keyboard or OS have the same keymapping, or you may disable other normal keys without knowing.
Caution 2: Note that if you've done everyhing above and then launch Chrome or Chromium in Kiosk Mode, you can't get out anymore! Only physically pressing the power button or killing the application over SSH or Telnet will let you resume normal operation again.
To make those changes permanent, read the end of this guide:
https://stackoverflow.com/a/11219056/3525780
EDIT: To those who have problems disabling the F1, F5, etc. keys, use following as a workaround:
xmodmap -e "keycode 67 = Escape"
(Somehow those "F keys" need to be assigned to an already existing and assigned key)
Having recently encountered the same kiosk-type problem (and not being able disable all keys in Chrome) I eventually found a solution which I thought I would share:
Using node-webkit I created the following package.json file:
{
"name" : "mykiosk",
"window" : {
"fullscreen" : true,
"toolbar" : false
},
"main" : "http://the-one-and-only-allowed.url/"
}
Launch with: ./nw
All function keys are blocked. Ctrl+N/T do not create tabs. It is quite nice
One last javascript/onload trick to disable the right-click context menu:
window.oncontextmenu = function(ev) {
ev.preventDefault();
ev.stopPropogation();
return false;
}
Chrome has Kiosk Mode, but that won't prevent users from using OS keyboard shortcuts (like ALT+F4, which aren't part of Chrome. Windows handles those). To start it in Kiosk Mode, run it using these parameters:
chrome.exe --kiosk http://www.google.com
My public library actually did something pretty awesome: they installed an extremely minimal Debian build on their kiosks, and run Google Chrome on each one. There are no close buttons, and no desktop to get into, so this deters virtually all the CTRL+ALT+DELETE hackers out there. ALT+F4 doesn't work either, and closing the browser by right-clicking opens up another one instantly.
But they forgot to get rid of GRUB's 10 second timeout, which lets users (well, me) get into recovery mode -_-, so I'm working with them to get that fixed...
I'd seriously consider Linux, as you can install it really quickly on multiple computers and basically forget about viruses and security. But the downside is that there isn't a "Administrator Panel" for you to tweak things with. You'd have to whip out nano (sorry, can't get used to vim) and edit some config files.
For me using version 52.0.2743 the --kiosk tag didn't work, but the --app="http://www.example.com" did what I wanted. (Disabled chrome keyboard shortcuts so I could use shellinabox + nano without issue.)
Also works on Chrome Canary (for which the --kiosk tag also didnt' work).
Other possibly helpful links:
Chrome support how to make a Kiosk App: https://support.google.com/chrome/a/answer/3316168?hl=en
Kiosk App for Chrome: https://chrome.google.com/webstore/detail/kiosk/afhcomalholahplbjhnmahkoekoijban?hl=en