userscript chrome vs. firefox - firefox

i have written a big userscript for greasemonkey which works just fine in firefox, but in chrome nothing happens :(
// ==UserScript==
// #name Name
// #description Desc.
// #author chiefwrigley
// #version 7.3
// #license (CC) chiefwrigley
// #namespace http://userscripts.org/scripts/show/103899
// #include *
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==
which functions can i use and which not? e.g. GM_setvalue... are there equal functions?

Use Tampermonkey. It allows almost all GM scripts to run on Chrome.
For a somewhat dated matrix of what Chrome userscripts allow, otherwise, start with this table (which needs updating).

You can check out the Greasemonkey Wiki to find out about cross browser compatibility. A good rule of thumb though is that other browsers don't really support any GM_* functionality.
For Chrome specifically, it looks like it doesn't support "#require, #resource, unsafeWindow, GM_registerMenuCommand, GM_setValue, or GM_getValue." and "GM_xmlhttpRequest is same-origin only." [Source]

If nothing is happening, and you're depending on jQuery, the likely cause is that chrome doesn't use #require, so jQuery isn't present... meaning your $(document).ready() is doing nothing, so the script never starts.
I've written a userscript/Greasemonkey pattern which will let you get jQuery (and UI, and whatever else you need) working, in both Chrome and FF, as well as Opera. http://userscripts.org/scripts/show/123588

Related

How to trigger Tampermonkey/Greasemonkey userscript in Slack web verision?

I'm having trouble getting any userscript to execute in Slack web interface.
The script is loaded with the page, as shown by Tampermonkey, but it doesn't even write into the console.
Even something totally basic like below has no effect.
// ==UserScript==
// #name New Userscript
// #namespace Namespace
// #version 0.1
// #description try to take over the world!
// #author You
// #match https://app.slack.com/*
// #grant none
// ==/UserScript==
(function() {
console.log('==================');
})();
I tried to use waitForKeyElements but with no success.
Is there a way around this?
EDIT:
It appears to be a problem specifically with Firefox (101.0 (64-bit)) & Tampermonkey 4.17.6161, as Edge (Version 102.0.1245.33 (Official build) (64-bit)) & Tampermonkey 4.16.1 works fine with the same userscript.
Okay, so letting you know that it worked for me. But there were many other lines that flooded the console (especially in a Slack channel), so I made sure the console log I added would be easy to notice. Here's what I added:
// ==UserScript==
// #name ___NewUserscript
// #namespace http://tampermonkey.net/
// #match https://app.slack.com/*
// #grant none
// ==/UserScript==
(function() {
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
console.log('**********************************************************************');
})();
In the console, my blocks of asterisks were added about 15 lines down, and then scads of lines appeared after that.
You are probably right that it's a Firefox problem - I tried my script with Brave browser (a secure variant of Chrome by the same guy who created Firefox) and it worked fine.
If that's the answer (i.e. Firefox vs Chrome/Edge), please add that as an answer and mark it as the correct answer in order to close out the question and make it easy for the next guy.
It appears to be a problem specifically with Firefox (101.0 (64-bit)) & Tampermonkey 4.17.6161, as Edge (Version 102.0.1245.33 (Official build) (64-bit)) & Tampermonkey 4.16.1 works fine with the same userscript.
There are few variables here, but I consider this an answer to this question sufficient to my current needs.

Getting Modernizr.mq working in IE 8

I'm using Modernizr.mq(http://modernizr.com/docs/#mq) to run checks in my JS, and I was under the impression that the Respond polyfill would make Modernizr.mq work in IE 8 as IE 8 does not support media queries but this isn't the case, see this test page in IE 8: http://goo.gl/ND9sA, the bg should be orange according to this:
if (Modernizr.mq('screen and (min-width: 650px)')) {
$('body').css('background', 'orange');
}
So does Respond only work for media queries specified within CSS and not JS? I thought Modernizr.mq uses matchMedia and Respond includes the matchMedia polyfill but still doesn't work.
So how do you get Modernizr.mq working for IE 8 or can't you?
Modernizr doesn't change the values of its tests if you load polyfills, so if the browser doesn't support media queries, Modernizr.mq() will return false, and adding the Respondjs polyfill won't change that.
Sorry about that.
Having said that, if you've got the polyfill loaded, then you're already doing all you can to support old browsers (Respondjs supports IE6/7/8), so there's not a huge amount more you can achieve with the Modernizr flag. , You theoretically shouldn't need to use the Modernizr test with the polyfill in place, as all browsers should support the feature anyway.

History issue combining WP7.5, phonegap and jqm

I have a phonegap app that uses jqm that works fine in android and ios.
Porting to WP7 i have an issue with the history, specifically history.back() (but also .go(-1) etc). This refers to going back in history where the previous 'page' was in the same physical html file, just a different data-role=page div.
using a jwm site in a regular browser is fine (with separate 'pages' in the same html file). Also, using history.back() when we go from one html file to another in the app is fine. It's the specific combination of WP7.5, jqm and PG.
Has anyone come across a solution for this? it's driving me crazy, and has been as issue since PG 1.4.1 and jwm 1.0.
EDIT 1: It's possible that the phonegap process of initialising the webview on WP7.5 somehow overrides the jqm history overrides, after they've loaded.
EDIT 2: definitely something to do with jqm not being able to modify the history. each time there is a 'page' change, history.length is still 0.
EDIT 3: When i inspect the 'history' object, i found there is no function for replaceState or pushState - i know jqm uses this for history nav, maybe that's the problem.
ok - this isn't perfect, but here's a solution (read: hack) that works for me. It only works for page hash changes, not actual url changes (but you could add a regex check for that). Put this somewhere in the code that runs on ondeviceready:
if (device.platform == 'WinCE') {
window.history.back = function () {
var p = $.mobile.urlHistory.getPrev();
if (p) {
$.mobile.changePage("#" + p.pageUrl, { reverse: true });
$.mobile.urlHistory.stack.splice(-2, 2);
$.mobile.urlHistory.activeIndex -= 2;
}
}
}

Flash + fancy uploader.. have to click twice on firefox

http://digitarald.de/project/fancyupload/3-0/showcase/attach-a-file/
That's the uploader plugin I'm using.
If you go there in firefox, you'll notice you have to click "attach a file" twice before it works. It seems to work fine in every other browser (that I've tested).
it's creating a flash object, and I'm not sure how to go about making it so you only click once in FF.
I'm not familiar with mooTools, but have you tried something like this? (attempted to write it in mooTools, but have no idea what I'm doing).
$('uploadLink').addEvent('click', function(){
if(Browser.firefox) $('uploadLink').fireEvent('click');
});
or I suppose if it has to wait for the flash to be created, something like this:
$('uploadLink').addEvent('click', function(){
if(Browser.firefox){
var flashTimer = setTimeout(function(){
clearTimeout(flashTimer);
/// or however you make sure the flash has successfully been added to the page
if($('flashContainer').getElements().length) $('uploadLink').fireEvent('click');
},100);
}
});
There's always the possibility that FF's security measures won't let you do something like this (mouse interactions with flash can be potentially harmful, as flash has FS access and stuff).
Depending on what your backend is, I'm highly in favor of skipping flash for file uploads when possible. One very well written plugin for such a task is available here:
http://valums.com/ajax-upload/
Good luck!

How to Stop the page loading in firefox programmatically?

I am running several tests with WebDriver and Firefox.
I'm running into a problem with the following command:
WebDriver.get(www.google.com);
With this command, WebDriver blocks till the onload event is fired. While this can normally takes seconds, it can take hours on websites which never finish loading.
What I'd like to do is stop loading the page after a certain timeout, somehow simulating Firefox's stop button.
I first tried execute the following JS code every time that I tried loading a page:
var loadTimeout=setTimeout(\"window.stop();\", 10000);
Unfortunately this doesn't work, probably because :
Because of the order in which scripts are loaded, the stop() method cannot stop the document in which it is contained from loading 1
UPDATE 1: I tried to use SquidProxy in order to add connect and request timeouts, but the problem persisted.
One weird thing that I found today is that one web site that never stopped loading on my machine (FF3.6 - 4.0 and Mac Os 10.6.7) loaded normally on other browsers and/or computers.
UPDATE 2: The problem apparently can be solved by telling Firefox not to load images. hopefully, everything will work after that...
I wish WebDriver had a better Chrome driver in order to use it. Firefox is disappointing me every day!
UPDATE 3: Selenium 2.9 added a new feature to handle cases where the driver appears to hang. This can be used with FirefoxProfile as follows:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("web");
firefoxProfile.setPreference("webdriver.load.strategy", "fast");
I'll post whether this works after I try it.
UPDATE 4: at the end none of the above methods worked. I end up "killing" the threads that are taking to long to finish. I am planing to try Ghostdriver which is a Remote WebDriver that uses PhantomJS as back-end. PhantomJS is a headless WebKit scriptable, so i expect not to have the problems of a real browser such as firefox. For people that are not obligate to use firefox(crawling purposes) i will update with the results
UPDATE 5: Time for an update. Using for 5 months the ghostdriver 1.1 instead FirefoxDriver i can say that i am really happy with his performance and stability. I got some cases where we have not the appropriate behaviour but looks like in general ghostdriver is stable enough. So if you need, like me, a browser for crawling/web scraping purposes i recomend you use ghostdriver instead firefox and xvfb which will give you several headaches...
I was able to get around this doing a few things.
First, set a timeout for the webdriver. E.g.,
WebDriver wd;
... initialize wd ...
wd.manage().timeouts().pageLoadTimeout(5000, TimeUnit.MILLISECONDS);
Second, when doing your get, wrap it around a TimeoutException. (I added a UnhandledAlertException catch there just for good measure.) E.g.,
for (int i = 0; i < 10; i++) {
try {
wd.get(url);
break;
} catch (org.openqa.selenium.TimeoutException te) {
((JavascriptExecutor)wd).executeScript("window.stop();");
} catch (UnhandledAlertException uae) {
Alert alert = wd.switchTo().alert();
alert.accept();
}
}
This basically tries to load the page, but if it times out, it forces the page to stop loading via javascript, then tries to get the page again. It might not help in your case, but it definitely helped in mine, particularly when doing a webdriver's getCurrentUrl() command, which can also take too long, have an alert, and require the page to stop loading before you get the url.
I've run into the same problem, and there's no general solution it seems. There is, however, a bug about it in their bug tracking system which you could 'star' to vote for it.
http://code.google.com/p/selenium/issues/detail?id=687
One of the comments on that bug has a workaround which may work for you - Basically, it creates a separate thread which waits for the required time, and then tries to simulate pressing escape in the browser, but that requires the browser window to be frontmost, which may be a problem.
http://code.google.com/p/selenium/issues/detail?id=687#c4
My solution is to use this class:
WebDriverBackedSelenium;
//When creating a new browser:
WebDriver driver = _initBrowser(); //Just returns firefox WebDriver
WebDriverBackedSelenium backedSelenuium =
new WebDriverBackedSelenium(driver,"about:blank");
//This code has to be put where a TimeOut is detected
//I use ExecutorService and Future<?> Object
void onTimeOut()
{
backedSelenuium.runScript("window.stop();");
}
It was a really tedious issue to solve. However, I am wondering why people are complicating it. I just did the following and the problem got resolved (perhaps got supported recently):
driver= webdriver.Firefox()
driver.set_page_load_timeout(5)
driver.get('somewebpage')
It worked for me using Firefox driver (and Chrome driver as well).
One weird thing that i found today is that one web site that never stop loading on my machine (FF3.6 - 4.0 and Mac Os 10.6.7), is stop loading NORMALy in Chrome in my machine and also in another Mac Os and Windows machines of some colleague of mine!
I think the problem is closely related to Firefox bugs. See this blog post for details. Maybe upgrade of FireFox to the latest version will solve your problem. Anyway I wish to see Selenium update that simulates the "stop" button...
Basically I set the browser timeout lower than my selenium hub, and then catch the error. And then stop the browser from loading, then continue the test.
webdriver.manage().timeouts().pageLoadTimeout(55000);
function handleError(err){
console.log(err.stack);
};
return webdriver.get(url).then(null,handleError).then(function () {
return webdriver.executeScript("return window.stop()");
});
Well , the following concept worked with me on Chrome , try the same:
1) Navigate to "about:blank"
2) get element "body"
3) on the elemënt , just Send Keys Ësc
Just in case someone else might be stuck with the same forever loading annoyance, you can use simple add-ons such as Killspinners for Firefox to do the job effortlessly.
Edit : This solution doesn't work if javascript is the problem. Then you could go for a Greasemonkey script such as :
// ==UserScript==
// #name auto kill
// #namespace default
// #description auto kill
// #include *
// #version 1
// #grant none
// ==/UserScript==
function sleep1() {
window.stop();
setTimeout(sleep1, 1500);
}
setTimeout(sleep1, 5000);

Resources