Get Meta Data content scripts in chrome extension [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I used chrome.tabs to get the meta data in content scripts for chrome extension. but it dosen't work because the chrome.tabs is only used in background script and popup script.
How do I have to do to get the meta data of active page in content scripts?
Thanks.

Since, as you noticed, a content script can't call tabs API, it need to ask something that can to do it.
That something is a background, or even better, event page.
Asking in this case means sending Messages. A message from a content script will be stamped with the Tab object of the page it comes from, so you don't need to look for it.
// Content script
chrome.runtime.sendMessage({action: "getTabDetails"}, function(response) {
// response.id is the tab ID
});
// Event script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action === "getTabDetails") {
// Here, sender.tab will be the tab you need; you can pass whatever data
// required back. For example, the ID:
sendResponse({id: sender.tab.id});
}
});

Related

'webRequest' event listener immediately failing in Chrome Extension [duplicate]

This question already has answers here:
"No matching signature" error on adding a chrome.webRequest listener
(1 answer)
Persistent Service Worker in Chrome Extension
(7 answers)
Closed 7 months ago.
My Goal
To write a browser extension which can use the webRequest permission. It needs to listen to the onResponseStarted event and read the response headers for the Content-Type. Based on specific content types, it will inject a content script that changes the content on the page.
Also, if there is an easier way to get the response headers, that would be preferred over getting this to work. See below for what I've already found.
My current position
Since I have only started on the extension, it is very simple. This is my manifest.json
{
// ... clutter like name and version removed ...
"manifest_version": 3,
"author": "Lakshya Raj",
"background": {
"service_worker": "handler.js"
},
"permissions": [
"webRequest"
]
}
And my handler.js
chrome.webRequest.onResponseStarted.addListener(function(details){
console.log(details);
});
I am on Windows 10, Google Chrome Version 103.0.5060.134 (Official Build) (64-bit).
Error(s)
In the extensions page, there are two errors listed for my extension (installed via "load unpacked")
(Warning) Service Worker Registration Failed. This shows a preview of my extension manifest with the text "handler.js" highlighted. No other information is provided.
(Error) Uncaught TypeError: No Matching Signature. This has two sections, context and stack trace. The content reads "extensions::webRequestEvent" (without formatting), while stack trace reads "Nothing to see here, move along."
Those aren't very useful error messages (at least to me), but that's all it says. I'm assuming someone on Stack Overflow has come upon this before and has a solution.
What I'm expecting
I expect that the details to be logged into the console when I navigate to a page, say https://stackoverflow.com/robots.txt. When the service worker is registered without errors, I can visit the extensions page page and click the link that reads "service worker". That takes me to the console where the output appears.
Stuff I've already researched
Apparently, it is really difficult to get response headers, but on continual search, I came upon this extension called Charset. Charset is able to get the response headers (as part of the functionality code), and it uses webRequest to do so. For this reason, I am trying the webRequest permission in my code. I also looked through the webRequest documentation and did a few unsuccessful Google searches.

Javascript trigger a URL on Homepage Load [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
We are moving our website from hosting.
I was wondering why the Youama extension is not loading on our homepage. This is our website in old hosting as you can see upon loading the homepage the extension is loaded right away. But in our new hosting which is an exact replica, no changes or modifications added, as you can see it's not loading the extension, but if you click the Login at the top it will run the extension.
Can someone here please help me identify the issue? Or maybe give me a solution wherein I can manually add a Javascript code so I can trigger such event?
Looking forward.
There are hard coded links to the old site, see the example below. Change those links to the new domain.
function isUserLoggedIn()
{
$.ajax({
//Update to http://electricbot.com/lipstickboutique/
url: "http://lipstickboutiquewholesale.co.uk/checkuser.php",
async: false,
cache: false,
timeout: 30000
})
.done(function(data){
userStatus = data;
})
;
}
There is a javascript error that blocks the Youama extension to execute.
Maybe you are still sending ajax request to old url: There is some kind of CORS prevention on the back-end, which results in ajax request failing.

Accessing all the window variables of the current tab in chrome extension [duplicate]

This question already has answers here:
Access variables and functions defined in page context using a content script
(6 answers)
Closed 7 years ago.
Can anyone post the code for accessing the window variables in chrome extension.
I need to access the window variables when i click the chrome extension button.
I am getting the window object, but not with all the variables loaded.
I know we can create it by injecting the script. But i am not getting how to achieve it.
Currently I am trying the following code to get the page source of the current active tab.
chrome.tabs.executeScript({code:
"document.getElementsByTagName('html')[0].innerHTML"
}, function(result) {
var value = result[0];
console.log(value);
chrome.tabs.query({currentWindow: true, active: true}, function(tabs) {
callback({
url: tabs[0].url,
title: tabs[0].title,
jsonValue: value
});
});
});
Please help me in solving this issue. It would be highly appreciated.
What you are asking for is to inject a script an get the value of a variable defined in the original page script. There are two answers to this problem:
1. Official Google answer
This is not possible. Content script injected in a page is sandboxed and can't access to original page javascript scope. This means that you can't access to variable, function and objects defined in the original page's javascript. And your variable, function and objects will not be accessible from the original page.
Only the DOM of the page is shared. That allow you to modify the content of the page. But you can't, for example, delete an existing event handler.
This is for evident security and safety reason. If you override without knowing it a function of the original page, it would break it.
Take a look here for more information
2. Unofficial and dirty answer
There is a way to bypass the chrome sand box restriction. This come with the shared DOM that allow you to add a <script src="..."><\script> to the page. The script will be loaded and executed in the original page's javascript VM so you will have access to the global javascript scope.
But this way, will not have access to the Chrome extension API, because you are running code in the original page. So the communication with the background page or the injected Content Script will be difficult.
A common way to do this is to add a hidden <div> to the page and put in it the result you want to send to your Content Script. You put the result as text (with JSON.stringify for example) and then read the result with your Content Script.
It's really dirty and it have to be use only in last try.

disable (Ctrl+U) keyboard script to prevent view source [duplicate]

This question already has answers here:
How to disable (View Source) and (Ctrl + C ) from my site
(10 answers)
Closed 9 years ago.
I'm looking for disable keyboard script to protect hidden content.
It is not possible. A user will always be able to view your source since he needs to download it in order to render the page.
There are more ways to view source than what you are trying to prevent:
Using firebug
Using wget
Right clicking on content choosing 'view source'
Using the menu option
Via man in the middle
probably more...
This is not very complicated but totally unreliable. Its same with all other Javascript protections.
First the trick (IE incompatible):
function denyKey(event) {
var code = event.keyCode;
if(event.ctrlKey) {
if(code==85)
return false;
}
}
window.addEventListener("keydown", denyKey);
My code is just scratch, it is not cross-browser. This is where to get keyCodes. I did not put much effort in the code since I want to discourage you from using it.
Once you send data to user, he can read the data unless you encrypt them without giving him the key. This means any:
Javascript authentication
Secret loading pages
Javascript "Wait before download..."
Blocked mouse buttons
..can and will be bypassed by the user.
I have a bookmarklet to unblock mouse buttons for example.
That is not possible, even if it was, it would have been a horrible protection. Even I could write a simple script that fetches the source of an arbitrary page. Everything that the client sees, is 'view source'-able (somebody edit that). Only server-side code is safe. Even if it was only possible to view your page through a real browser (but you can't make it so) you'll probably overlook a accelerator key, or other shortcut. If you don't want the client to see some code, don't give it to him! Keep it server-side (and not in a .txt file, that's accessible too) or don't keep it.

load page and execute javascript in a url

Hello wonderful stackoverflow users.
I have a question about url loading.
In many browsers and web viewers, there is the functionality to load a url to a website, but also a url to execute javascript.
Load a website: http://www.google.com
Load a script: javascript:alert("Hello!");
My question is, is there a way to load an http request as well as a javascript.
The answer is most likely no, but I want to confirm because I can't find any resources that describe this.
I was thinking it would be something like:
http://www.google.com&&javascript:alert("Hello!");
but the problem is, of course, this is not correct.
The reason why I am doing this is to provide a url that once it is clicked, it will also execute a certain javascript function. This will be in Android.
I appreciate any response, and understand that the answer may be no.
It all depends on whether you have control of the page being linked to. If you cannot modify the source of the linked page, then the answer is quite simply, no.
But, if it is your page, you can pass arguments in the hash, and then read the hash when the page loads and execute script accordingly.
window.onload = function () {
if (location.hash.indexOf("doSomething") > -1) {
// do something
}
};
You can execute javascript when a page loads using Browser plugins, such as GreaseMonkey for Firefox, or TamperMonkey for Chrome.
https://addons.mozilla.org/en-us/firefox/addon/greasemonkey/
http://tampermonkey.net/index.php?version=3.11&ext=dhdg&updated=true

Categories

Resources