My Greasemonkey script runs on every site except the one I want it to? - greasemonkey

I'm trying to set up a script to run on Quora, but it's running on every other site instead.
I have the #include directive in the metadata block, and under script settings/include, it's set to include *, and I can't see any way to remove that.
Here's a thread about the same issue I'm having, but there's no #exclude directive to remove (although I did try putting #exclude * before the #include, but that didn't fix anything). I also checked my formatting against the answer here and I don't see any typos.
Here's my metadata block.
// ==UserScript==
// #name Quora fixer
// #namespace quora
// #include https://www.quora.com/*
// #version 1
// #grant none
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// #description prevents links from opening in a new tab
// #run-at document-idle
// ==UserScript==

First, see and follow My very simple Greasemonkey script is not running?.
If the script is running on every other page, and there are no Quora-specific #excludes, then the script most-likely is running on Quora -- just not doing what you expect.
Other issues or actions:
You are using #grant none and #require-ing jQuery. This leads to conflicts and javascript crashes on sites like Quora. Use #grant GM_addStyle.
If the Script Settings tab (not to be confused with the User Settings tab) shows differently than the actual script source, then either you are not looking at the same file Greasemonkey is, or Greasemonkey and/or Firefox has become corrupted/confused. In that case:
Uninstall the Greasemonkey script.
Completely clear the browser cache.
Shutdown Firefox completely. Use Task Manager, or equivalent, to verify that there is no Firefox thread/task/process in memory.
Go to your Firefox profile folder.
Enter the gm_scripts folder therein.
If there are any subfolders left over from the script you just uninstalled, delete them. In your case, the folders should have names similar to Quora_fixer.
Restart Firefox.
Make sure the script is not listed.
Install the Greasemonkey script afresh.
If it still doesn't work, create a new Firefox profile or try a different computer altogether.
Once the script fires on Quora and no relevant errors show on Firefox's Browser Console (see the first link above), then if the script still does not do what you expect, you may need to apply AJAX-aware techniques.

Related

#exclude in userscript not working as expected

I have a userscript that I would like to run for a particular domain, but not for a particular page.
For example, I want to stop the script from running when the URL contains documentMode=edit:
https://blah.itglue.com/2443511/docs/8093867#documentMode=edit&version=draft
I tried this:
// #exclude https://blah.itglue.com/*/docs/*#documentMode=edit
// #exclude https://blah.itglue.com/.*/docs/.*documentMode=edit&.*
// #match https://blah.itglue.com/*
However, the script still runs and I'm not sure what I'm doing wrong. I can see the URL added to exclude in Tampermonkey.
I checked this in a regex live editor and the above URL matches perfectly. I refreshed the page but the userscript is still applied....
However, if I use this, it stops the script loading on the page:
/(^[^:\/#\?]*:\/\/([^#\?\/]*\.)?blah\.itglue\.com(:[0-9]{1,5})?\/.*$)/
I don't want the script to stop working on the root domain though.....

Can you make a node server code communicate with HTML of a website open in browser?

For example I've written a code to access a global variable of a web page and access values from it and then I want to put that value in some HTML div of that page. Now I can copy and paste this code into browser console and it will work.
But instead of pasting it in console, is there any way I can run my own server (localhost) using NodeJS and do the same from there? I mean communicate to the browser and that page which is open?
If yes, what things will I need?
I would use selenium-webdriver
It takes a bit of setup, but afterwards it's pretty easy
go here and click on the folder just above the one that says 'icons' (this should be the latest version of chrome webdriver)
Then, once you download the latest version, drag the exe C:\WINDOWS (if you're not on windows just move it to any folder on the PATH environment variable
now that you've done that, set up a simple script like this:
const webdriver = require('selenium-webdriver')
let driver = new webdriver.Builder().forBrowser('chrome').build();
(async function example() {
await driver.get('http://example.com/')
await driver.executeScript('/* your code, for example: */ document.getElementsByTagName(\'h1\')[0].innerHTML = \'test\'; console.log(\'test\')')
}())
make sure to escape (but a backslash before) any single quotes in your driver.executeScript
I would recommend learning a bit about selenium-webdrivers api so that you can to more complicated things (such as running this without actually opening a new window). I would also that you use selenium-webdriver's api (which allows you to edit webpages, click things, input keys, etc.) instead of just putting everything in an executeScript as much as you can.

How can one debug the Chrome extension "options" page using the new OptionsV2 method?

https://developer.chrome.com/extensions/optionsV2 tells me that I should be using options_ui in my manifest, rather than options_page, and recommends I start upgrading immediately.
However, I can't find any way to actually debug the script run by my options page when I use options_ui—the Options popup is in an tag, and the developer tools don't show me the source, or even the HTML content.
For now, I just comment out options_ui and let options_page take effect when I need to debug. I'm guessing that setting "options_ui": {"open_in_tab": true,...} would have the same effect, but it would be really nice to figure out how to actually debug the script when it's running the new way.
Auspex,
Teepeemm's comment is correct.
Other way, you can launch your options page from other tab using its full URL
like,
chrome-extension://{your extension id here}/{your options page path here, from the extension root}
e.g. say my extension id aaabbbcccdddeeefffggg, and say, my options page is located (from extension root) at app/html/options.html; then i can load up below URL in a new tab ---
chrome-extension://aaabbbcccdddeeefffggg/app/html/options.html
Now here, in this tab; you can do your regular debugging around HTML and javascript.
I hope this suffices your debugging requirement for 'new options UI' for chrome.
Teepeemm's comment is correct.
It's as simple as right-clicking inside the options page modal and selecting "Inspect element" - it will open the correct Dev Tools.

Greasemonkey #exclude does nothing

Greasemonkey script excludes do nothing, for some reason. They do not even seem to be recognized by Greasemonkey, and I would like to know why.
I have to go into the preferences for each script and add the excludes manually if I want the desired behavior. Apparently I'm getting the syntax correct, since a simple script like (there's nothing special about my choice of domain names, and I've checked it with others):
// ==UserScript==
// #name Delete Adsense Junk
// #exclude http://en.wikibooks.org/*
// #exclude https://en.wikibooks.org/*
// #include http://weather.org/*
// #include https://weather.org/*
// #version 1
// ==/UserScript==
alert("Alert!");
tells me all about where the script will and won't execute when I go to install it. Firefox wants to run the script on every page unless I navigate to the preferences for that script and change the user excludes though. The list of script excludes is empty and the include is just the wildcard. Is there some option that should be enabled/disabled that I'm missing? The user excludes seem to work normally. I'm just trying to figure out why Greasemonkey is ignoring the directives that I put in the script.
The script runs on the page and on all its frames. The alert box tells you which domain it is from (at least in FF). For example on YouTube it also runs for https://plus.google.com and https://s.ytimg.com. So for YouTube I get a total of 3 alerts and if youtube.com is excluded still 2 alerts.
Check out this post to see how to exclude them.

change Watir browser headers

How is it possible to change browser header with Watir?
I'd like to change browser headers (in Firefox or Chrome) when using Watir.
I know about watir-user-agent gem, but I'm interested in changing browser version.
Is that possible?
Thanks
Yes this can be done.
Unfortunately Watir does not seem to provide any very easy way to do this
However, here are 2 simple options which work:
A. Use a proxy server.
This is a well understood way to modify headers generally. However I have not personally used it during automation.
Steps :
1. Setup proxy server before your test code is executed
2. Ensure the proxy server will add the required headers to every request
3. Then when your test browser requests any page ----> the proxy server will automatically add the required headers.
B. Use browser extensions
Since Watir cannot seem to to modify headers by itself ... then we just ask Watir to use a normal browser extension which can!
I have done this successfully using Chrome and firefox
Note: These steps work with ONLY the indicated extensions - but a similar approach should also work fine for many other extensions.
Firefox Steps:
1. Start firefox
2. Search for 'Modify Headers Firefox' using a very popular search engine .... the top result is https://addons.mozilla.org/En-us/firefox/addon/modify-headers/
3. Download the .xpi file for this extension ... currently you can do this by right clicking on the button and clicking "save link as"
4. Install the extension as normal, change the headers as you wish, close firefox, then locate and save the "modifyheaders.conf" file ... this file should be somewhere in your user folder
5. Make the following class (which extends Profile)
class FirefoxProfileWithAddedFiles < Selenium::WebDriver::Firefox::Profile
# This method OVERRIDES the one in Profile
# This method creates the firefox profile folder
def layout_on_disk
#Call the superclass layout method
profile_directory = super
#Add custom file
if(!#file_to_add_to_profile.nil?)
FileUtils.cp(#file_to_add_to_profile, profile_directory)
end
profile_directory
end
def add_file_to_profile(filepath)
#file_to_add_to_profile = filepath
end
6. Set your test script up as follows
...
#Setup Firefox Profile
profile = Selenium::WebDriver::Firefox::Profile::FirefoxProfileWithAddedFiles.new
profile.add_extension("SOMEPATH/modifyheaders.xpi")
profile.add_file_to_profile("SOMEPATH/modifyheaders.conf")
profile["modifyheaders.config.active"] = true
#Start up Firefox
#browser = Watir::Browser.new :firefox, :profile => profile
...
Chrome Steps
1. Start chrome
2. Search for 'Modify Headers Firefox' using a very popular search engine .... the top result is https://chrome.google.com/webstore/detail/modify-headers-for-google/innpjfdalfhpcoinfnehdnbkglpmogdi
3. Install the extension as normal, change the headers as you wish, then close chrome
4. Locate the unpacked extension folder and copy it. On windows the folder will be something like...
C:\Users\MYNAME\AppData\Local\Google\Chrome\User Data\Default\Extensions\innpjfdalfhpcoinfnehdnbkglpmogdi\2.0.3_0
5. Locate the extension configuration file and copy it. On windows, the file will be something like...
C:\Users\MYNAME\AppData\Local\Google\Chrome\User Data\Default\Local Storage\chrome-extension_innpjfdalfhpcoinfnehdnbkglpmogdi_0.localstorage
6. Set your test script up as follows:
...
#Setup Chrome Profile Folder
profile_directory = Dir.mktmpdir("webdriver-chrome-profile")
extension_configuration_folder = FileUtils.mkdir_p "#{profile_directory}/Default/Local Storage"
FileUtils.cp("PATH_TO_MY_EXTENSION_CONFIGURATION_FILE", extension_configuration_folder[0])
#Start Webdriver
#browser = Watir::Browser.new :chrome, :switches => ["--user-data-dir=#{profile_directory}", "--load-extension=#{PATH_TO_MY_UNPACKED_EXTENSION_FOLDER"]
...
Watir automates the browser INSIDE the browser window, with very limited interaction up at the OS level (such as responding to alerts, etc), you would need to pre-configure the browser (presuming that was possible) to what you wanted, or use a tool such as Autoit to interact with the browser's OS level controls to do that.. (presuming the browser even has the feature to allow you to alter what it is reporting in terms of browser and version when it makes a request to a website)
If you are using Watir-Webdriver along with Firefox then you may be able to do this via a profile that sets those parameters. In that case you create the profile, then the browser object with that profile specified. it's pretty much a webdriver function, but easy enough to access when creating the browser object.
See this webdriver bug for the parameters to use (down in the comments) when creating the profile. Refer to webdriver docs for more info on how to setup and use profiles for firefox.
Another option that might be useful would be to fork your own version of the code for the user-agent gem and add browser_version as one of the things to be set. It's using profiles for FF, so doing that should be possible, at least for FF. for Chrome it is using the user-agent switch to override the useragent string, so it should be possible there also, although you would have to do a little work to modified the fixed strings the gem uses to replace the portion that has the version with the one you want.
Then if you get it working issue a pull request to add that enhancement to the gem..
or if you are not up to that sort of thing yourself, then beg, plead and offer to bribe the gem author with something appropriate if they would extend the gem for you to make version one of the things that could be set.

Resources