Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8056769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:49:15+00:00 2026-06-05T08:49:15+00:00

I am trying to write a greasemonkey javascript code for specifically my own page.

  • 0

I am trying to write a greasemonkey javascript code for specifically my own page. In the page, there is an img with id “barcode”.

I can use

var imageattributes = document.getElementById('barcode');

and get the image from the page.
What I want is that I want to be able to get all the pixels in this image in an array.

I have been going around in stackoverflow, and saw some ways to do this. I also saw pixastic. My problem with the solutions in stackoverflow is that

var imgData = context.getImageData(0, 0, canvas.height, canvas.width);

does not work. No matter what I do.
And pixastic does not show how I can view the pixel information anywhere. Their documentation only covers the functions they already have for inversion, blur, etc.

The code I currently have is this:

var imageattributes = document.getElementById('barcode').attributes;
var imageurl = imageattributes.getNamedItem("src").value;
var imageurl2 = imageurl.replace("..","http://localhost");

var img = new Image();
img.src = imageurl2;
context.drawImage(img, 0, 0);
var imgData = context.getImageData(0, 0, canvas.height, canvas.width);
var pixel = new Array();
for(i=0;i<canvas.height;i++){
    pixel[i] = new Array();
    for(j=0;j<canvas.width;j++){
        pixel[i][j] = imgData.data[i*canvas.width+j*4];
    }
}
document.getElementById('derp').innerHTML = imageurl;

Which is supposed to get the image, take it’s src, load it as a new image, and put the pixels inside an array. And the last line is just to show on the screen if the code has worked or not, so if the url prints, it means it worked, if it doesn’t, it doesn’t.

Can anyone please help me with this? Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:49:16+00:00Added an answer on June 5, 2026 at 8:49 am

    You don’t have (or aren’t using) a <canvas> element. Your HTML should look something like this:

    <img id="barcode" src="whatever.bmp">
    <canvas id="mycanvas">
        <div id="fallback">
            You only see this message if your browser doesn't support canvas.
        </div>
    <canvas>
    

    And your function should be prefixed with:

    var context = document.getElementById('mycanvas').getContext('2d');
    

    That should make your code function properly. You need to get the drawing context of a <canvas> element first, then add the image to that context, and finally retrieve pixel data from the context.

    To directly address your comment above, the line

    var context = document.getElementById('barcode').getContext('2d');
    

    tries to get the context of an image element, but that is not possible. You can only call getContext on a canvas element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a Greasemonkey script, and would like to use the jQuery
I'm trying to write a Greasemonkey script that alters keywords in Twitter posts. Trouble
Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write a code at the moment that basically tests to see if
Trying to write a javascript bookmarklet to jump from a front end link into
I'm trying to write a greasemonkey script that only displays photos with the tags
I'm trying to write a Greasemonkey script for Facebook and having some trouble with
I'm trying to write a Greasemonkey script for a hierarchy of websites such that
Trying to write a code that searches hash values for specific string's (input by
I'm trying to write a Greasemonkey script to update the onclick value of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.