Skip to content

Instantly share code, notes, and snippets.

@Farow
Last active October 15, 2016 21:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Farow/7684bddd54517a3fbfe9 to your computer and use it in GitHub Desktop.
Save Farow/7684bddd54517a3fbfe9 to your computer and use it in GitHub Desktop.
Bing Images Direct
// ==UserScript==
// @name Bing Images Direct
// @namespace https://github.com/Farow/userscripts
// @description Removes the preview popup and links lead directly to the images
// @include http*://www.bing.com/images/search*
// @include http*://www.bing.com/videos/search*
// @version 1.0.2
// @grant none
// ==/UserScript==
/*
changelog:
2015-08-27 - 1.0.2 - fixed issue when changing image size
2015-08-06 - 1.0.1 - added support for videos
2015-04-20 - 1.0.0 - initial release
*/
var observer = new MutationObserver(image_observer);
init();
function init() {
var wrapper = document.getElementById('dg_c');
if (!wrapper) {
/* we could be inside an iframe, check parent */
wrapper = window.parent._d.getElementById('dg_c');
/* failure */
if (!wrapper) {
return;
}
}
var images = wrapper.getElementsByClassName('dg_u');
for (var i = 0; i < images.length; i++) {
make_direct(images[i].children[0]);
}
observer.observe(wrapper, { childList: true });
}
function make_direct(image) {
var url;
/* for images */
if (image.hasAttribute('m')) {
url = image.getAttribute('m').match(/imgurl:"([^"]+?)",/)[1];
}
/* for videos */
else {
url = image.getAttribute('vrhm').match(/"p":"([^"]+?)"/)[1];
}
image.outerHTML = '<a href="' + url + '">' + image.innerHTML + '</a>';
}
function image_observer(mutations) {
mutations.forEach(function(mutation) {
for (var i = 0; i < mutation.addedNodes.length; i++) {
var images = mutation.addedNodes[i].getElementsByClassName('dg_u');
for (var k = 0; k < images.length; k++) {
make_direct(images[k].children[0]);
}
}
});
}
@HardKoreG
Copy link

Hi, I am HardKoreG on Reddit. I requested this script. I found a bug; When I search in images, it works fine, but if I click on a suggested searches below the search bar, the new search results have overlays and don't go to the direct image. Almost like it disables the script. For now, I have to refresh the page to get the script to work. Could you look into this and fix it? Thanks for making the script.

@Decme
Copy link

Decme commented Aug 6, 2015

Works with normal Bing images search, but seems to break if you choose a custom setting in Bing, such as "large" images (first option on the left)

Can you host this on a userscript site such as https://greasyfork.org/en so that we can get automatic updates and fixes if/when you update?

@Farow
Copy link
Author

Farow commented Aug 27, 2015

I might post this on oujs at some point. But try not to comment here. I get no notifications on new comments and found this entirely by chance. Contact me on reddit instead.

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