I'm trying to use drawImage on Canvas Context in injected content script from my Chrome Extension.
testCanvas = document.createElement('canvas');
testContext = testCanvas.getContext('2d');
var image = new Image();
testContext.drawImage(image, 0, 0);
In Chrome 26 it works ok, but in dev channel (Chrome 28) this seams broken as I got this message:
Uncaught TypeError: Type error
When I move same script directly into background page, it works without any problem.
I think this can be related to some security related change but I failed to find any relevant information.
This is a bug, you should report it. Some more testing reveals that in Chrome 28.0.1498.0, the Image constructor does not create a valid HTMLImageElement instance (as seen in the screenshot below).
This code is run in the context of a Content script. The same code works fine on regular pages and in the extension's process (background page).
To work around the problem, use document.createElement('img') instead of new Image().
And don't forget to report the bug at https://code.google.com/p/chromium/issues/list.
Related
I am using manifest version 3 for chrome extension this error I face in background js :
Error in event handler: ReferenceError: window is not defined chrome extension with manifest v3
"manifest_version":3,
"permissions":["contextMenus","storage", "activeTab","tabs","scripting","webRequest"],
var posLeft = ( window.width - winWidth ) / 2 ;
ManifestV3 extension uses a service worker so it doesn't have DOM or window.
Use chrome.windows.getCurrent to get the size/position of the current browser window.
Use chrome.system.display.getInfo (since Chrome 94) to get the display's size/metrics.
Well for others who may look here for that error message in a similar context, I got the same error when neglecting to make the window object accessible at runtime rather than at the time that the injected function is dynamically being readied for injection into a particular tab by the v3 background script.
In order to get dynamically injected from a v3 background script, the tab specific object (in this case window) needs to be included inside the function being passed, as in the following anonymous function case:
chrome.scripting.executeScript({
target: { tabId: currentTab.id },
func: () => window.history.back()
});
if window.history.back is provided as the value for func then obviously it will not be known or available to the background script and the same error message will ensue.
This is already described indeed in the docs.
If you are trying to access window object in background.js as it is a service worker you won't have access to window object , but you may try self as it will have all the properties of window object
in background.js try
console.log(self,"self")
var window = window ?? self;
Note: if you are using Vite or Webpack this might work
I'm trying to write a Chrome/Firefox extension in inject MathJax 3.0.1 into arbitrary web pages, Green Pi. It's working well for Chrome, but I'm having trouble with Firefox.
The content_script.js is
MathJax = {
chtml: {
fontURL: chrome.runtime.getURL("fonts"),
},
};
require("mathjax-full/components/src/tex-chtml/tex-chtml.js");
// This paints pages green
// require("green.js");
// // cat green.js == document.body.style.backgroundColor = "green";
(It's getting a little more involved when the user opts in/out of certain pages, but this isn't relevant here.)
Now, as noted in the code, the above works fine in Chrome, but fails in Firefox with the MathJax error
MathJax(?): window.MathJax is undefined
I don't see any other warning or error. Any hint on what might be going wrong here?
This turned out to be a MathJax bug after all, cf. https://github.com/mathjax/MathJax/issues/2399.
I generate pdf file from a HTML-page via jspdf plugin addHTML.
It works but the rendered text / font is really blurry, the original HTML page is not. Rendered images are fine, only text is the problem (see attached images).
original_image: http://111900.test-my-website.de/stackoverflow/orig.jpg
blurry_image: http://111900.test-my-website.de/stackoverflow/blurry.jpg
I read all google results the last three days - maybe I am the only person in the world I have exact this problem?!?! :/
I added the following scripts in my code:
spdf.js
jspdf.plugin.from_html.js
jspdf.plugin.split_text_to_size.js
jspdf.plugin.standard_fonts_metrics.js
pdf generation code:
pdf.addHTML(document.getElementById("container"),10,15,function() {
var string = pdf.save(filename);
});
Is there a quality option in jspdf I missed?
How can I render the font?
Thanks for reply,
Thomas
I found that when creating a PDF and the text was blurred when using addHtml this was because of the width of the web page. Try using it with the browser not maximised as a test.
My solution was to add some styles to adjust the width before calling addHTML with a width parameter that matches the styles I added. I then remove the additional styles in the function that runs after addHTML.
I had the same problem and I resolved it.
Actually, the main issue here is to specify the 'dpi' to avoid having a blurred image. In addition to that, try to avoid any 'smoothening' features beacuse it may make it worse. I have taken a look around the API and other discussion about it and I came back with the following solution:
1- update your version of html2canvas : many blurring issues have been fixed after the 1.0.0-alpha release.
2- use the following properties :
const context = canvas.getContext('2d');
context.scale(2, 2);
context['dpi'] = 144;
context['imageSmoothingEnabled'] = false;
context['mozImageSmoothingEnabled'] = false;
context['oImageSmoothingEnabled'] = false;
context['webkitImageSmoothingEnabled'] = false;
context['msImageSmoothingEnabled'] = false;
I have a google chrome extension that shares some code between it's content script and background process / popup. If it some easy and straightforward way for this code to check if it's executed as content script or not? (message passing behavior differs).
I can include additional "marker" javascript in manifest or call some chrome fnction unavailable from content script and check for exceptions - but these methods looks awkward to be. Maybe it's some easy and clean way to make this check?
To check whether or not your script is running as a content script, check if it is not being executed on a chrome-extension scheme.
if (location.protocol == 'chrome-extension:') {
// Running in the extension's process
// Background-specific code (actually, it could also be a popup/options page)
} else {
// Content script code
}
If you further want to know if you're running in a background page, use chrome.extension.getBackgroundPage()=== window. If it's true, the code is running in the background. If not, you're running in the context of a popup / options page / ...
(If you want to detect if the code is running in the context of an extension, ie not in the context of a regular web page, check if chrome.extension exists.)
Explanation of revised answer
Previously, my answer suggested to check whether background-specific APIs such as chrome.tabs were defined. Since Chrome 27 / Opera 15, this approach comes with an unwanted side-effect: Even if you don't use the method, the following error is logged to the console (at most once per page load per API):
chrome.tabs is not available: You do not have permission to access this API. Ensure that the required permission or manifest property is included in your manifest.json.
This doesn't affect your code (!!chrome.tabs will still be false), but users (developers) may get annoyed, and uninstall your extension.
The function chrome.extension.getBackgroundPage is not defined at all in content scripts, so alone it can be used to detect whether the code is running in a content script:
if (chrome.extension.getBackgroundPage) {
// background page, options page, popup, etc
} else {
// content script
}
There are more robust ways to detect each context separately in a module I wrote
function runningScript() {
// This function will return the currently running script of a Chrome extension
if (location.protocol == 'chrome-extension:') {
if (location.pathname == "/_generated_background_page.html")
return "background";
else
return location.pathname; // Will return "/popup.html" if that is the name of your popup
}
else
return "content";
}
Why is the following example not working in ie9?
http://jsfiddle.net/dzyyd/2/
It spits out a console error:
"Unexpected call to method or property access."
I found it pretty quickly. You created the element, but did not put it anywhere. Once it is added to the document body, everything seems to be fine.
this._width=300;
this._height=300;
this._bgSvgContainer = document.createElement("div");
//NOTE: add the created div to the body of the document so that it is displayed
document.body.appendChild(this._bgSvgContainer);
var bgCanvas = Raphael(this._bgSvgContainer, this._width, this._height);
this._bgCanvas = bgCanvas;
var num = this._bgCanvas.text(this._width-10,this._height-10,"1");
It's really hard to tell with such a tiny code-fragment (doesn't run on any browser for me), but it's probably a scope issue this in IE during events is completely different to this using the W3C event model. See: quirksmode-Event order-Problems of the Microsoft model