Giter Site home page Giter Site logo

invert selected images about invertimage HOT 5 OPEN

milahu avatar milahu commented on July 20, 2024
invert selected images

from invertimage.

Comments (5)

vycb avatar vycb commented on July 20, 2024

there is a some sort of hack, multiple images can be inverted by selecting and inverting a parent element of the images on the page

from invertimage.

milahu avatar milahu commented on July 20, 2024

not working on the sample page

when i right-click on a whole paragraph and run "invert images"
then images are inverted, but also all the text is inverted

screenshot:

firefox plugin invert images request invert selection

i expect the "invert images" plugin to only invert images, not invert all html tags

or did i miss something?

from invertimage.

vycb avatar vycb commented on July 20, 2024

yes, this is unique style on unique site.
img1

For this purpose Stylus addon can be used. For example: after inverting a parent element on the page I turned on style written in Stylus
#bookchapter {
color: #220;
}
img2

from invertimage.

milahu avatar milahu commented on July 20, 2024

same effect with only stylus addon:

@-moz-document url-prefix("https://www.dsprelated.com/freebooks/mdft/") {
	div#bookchapter img {
		filter: invert(1);
		-webkit-filter: invert(1);
		background: #fff;
	}
}

but i still want a "point and click" solution ....

i did a quick edit of your code, to optimize for speed and readability, and to "only invert images"

code is not tested, feel free to use it = no warranty + public domain

// target node from "context menu" click
var clicked_node = null // not a dictionary

// css code to invert image
// we use the css comments /*[*/..../*]*/ for more uniqueness
// so we dont accidentaly replace any original css code
// at the cost of a slower s.indexOf(invert_style)
const invert_style = "/*[*/;filter:invert(1);/*]*/"

browser.runtime.onMessage.addListener(function(msg){
	if (msg.cmd !== "invert") return

	// css selectors for ancestor node
	// TODO sort by frequency, most frequent first
	// TODO maybe remove to avoid "magic numbers"
	const invert_ancestors = [
		".media-image",
		"#AdaptiveMedia"
		".css-1dbjc4n",
		".rn-156q2ks",
		".rn-16y2uox",
		".rn-10m9thr",
		"._26ii _-_b",
		"._2a2q",
		"._2rea",
		"._5rgt",
		"._5dec",
		".uiScaledImageContainer",
		".tcu-imageWrapper",
		"._5rgu",
		"._3x-2",
	]

	// search ancestor node
	var ancestor_node = null // not a list
	for(let s of invert_ancestors){
		ancestor_node = clicked_node.closest(s)
		if(ancestor_node){
			toggle_invert(ancestor_node)
			return
		}
	}
	
	// ancestor node was not found
	// invert images in clicked node
	for(let i of clicked_node.getElementsByTagName("img")){
		toggle_invert(i)
	}

	//// ancestor node was not found
	//// invert clicked node
	//toggle_invert(clicked_node)

})



function toggle_invert(e){
	var s = e.getAttribute("style")
	var i = -1
	if(!s){
		// set style
		s = invert_style
	}else{
		i = s.indexOf(invert_style)
		if(i == -1){
			// append style
			s = s + invert_style
		}else{
			// delete style
			// re-use the result of s.indexOf(invert_style)
			s = s.substr(0, i) + s.substr(i + invert_style.length)
		}
	}
	e.setAttribute("style", s)
}

addEventListener('contextmenu', function (e) {
	clicked_node = e.target
}, false)

from invertimage.

vycb avatar vycb commented on July 20, 2024

Thanks for advice. This addon made for very general case in mind, to work with all type of web pages and elements. Even common case to invert video element or whole pdf document.

from invertimage.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.