In my code, I wish to post to two php files using a cookie from the website it is posting to (meepcity.com). However, upon loading the chrome extension, I receive the following two errors:
Error in response to cookies.get: TypeError: Cannot read property 'value' of undefined at token
As well as
Unchecked runtime.lastError while running cookies.get: No host permissions for cookies at url: "http://www.meepcity/". at token
I do not see anything wrong with my code, though I assume the problem has to do with the retrieval of the cookies. I have included my code as well as my manifest below. Thanks!
function token(domain, name, callback) {
chrome.cookies.get({"url": domain, "name": name}, function(cookie) {
if(callback) {
callback(cookie.value);
}
});
}
token("http://www.meepcity", "PHPSESSID", function(secureToken) {
function buy(id, security) {
$.ajax({url:"http://api.meepcity.com/prepareAssetPurchase.php",type:"POST",data:{sess:security,aId:id,sId:0}})
$.ajax({url:"https://api.meepcity.com/finishAssetPurchase.php",type:"POST",data:{sess:security}}).done(function() { console.info("Successfully purchased!"); });
}
buy(44,secureToken);
});
Manifest
{
"background": {
"scripts": [ "jquery.js", "background.js" ]
},
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"description": "Meepcity",
"homepage_url": "http://www.meepcity.com/",
"incognito": "split",
"manifest_version": 2,
"name": "Meepcity",
"permissions": [ "unlimitedStorage", "tabs", "notifications", "tabCapture", "*://*.meepcity.com/*", "https://*.meepcity.com/*", "cookies", "background" ],
"short_name": "Meepcity",
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "1.0.1",
"web_accessible_resources": [ "*://*.meepcity.com/*", "https://*.meepcity.com/*" ]
}
Thanks, I appreciate it.
Not sure, but I think you should do this:
Change "http://www.meepcity" to "https://www.meepcity.com"
Change {"url": domain, "name": name} to {"url": domain, "name": name, secure: true}
Hope it works
Related
I would like to send a message from popup to content script and to log an object once I get the response from this end but I'm getting the following error: Could not establish connection. Receiving end does not exist.
Why might this error be occurring and how should I fix it? Any help would be appreciated.
Besides the below code, I tried the workaround suggested on this link (https://www.youtube.com/watch?v=jooc7xMkz2E), by implementing the communication between the popup and content-script with long-lived connections and adding an event listener on load to the browser window but it did not worked either.
Implemented a similar function for sending the message to the background but without passing the tabs as an argument and it is logging the response from the background to the popup but not from content-script to popup, the error appears when I try to send a message from popup to content script and logging the respective response.
Below are the main project files.
Popup.ts:
ngOnInit(): void {
...
this.sendMessageToContentScript();
}
sendMessageToContentScript() {
chrome.tabs.query({currentWindow: true, active: true} , function (tabs){
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, { command: 'HelloFromPopup1' }, (response) => {
var parsed_res = JSON.parse(response);
console.log("content-script: " + parsed_res);
});
});
}
content_script.ts:
chrome.runtime.onMessage.addListener(
function(msg, sender, sendResponse) {
if (msg.command == "HelloFromPopup1") {
var personal_data = {
firstname: "Max",
lastname: "Hennigan",
role: "role",
location: "United Kingdom"
};
sendResponse(JSON.stringify(personal_data));
}
}
);
Manifest.json:
{
"name": "Extension name",
"version": "1.0",
"description": "Desc",
"manifest_version": 2,
"permissions": [
"activeTab",
"webNavigation",
"tabs",
"storage",
"http://*/",
"https://*/"
],
"background": {
"persistent": true,
"scripts": [
"background.js",
"runtime.js"
]
},
"browser_action": {
"default_popup": "index.html#/home",
"default_icon": "assets/icons/favicon-32x32.png",
"default_title": "title"
},
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": [ "content_script.js" ]
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
app-routing.module.ts:
const routes: Routes = [
{path: 'home', component: HomeComponent}
];
#NgModule({
imports: [RouterModule.forRoot(routes, {useHash: true})],
exports: [RouterModule]
})
Edited:
Following the suggestion of wOxxOm on the comments, I've tried to add programmatic injection to my popup.ts but it is giving me a response of "1" from the content script, it is not logging the object as it should.
I have tried it like this:
Popup.ts (with programmatic injection):
sendMessageToContentScript() {
chrome.tabs.query({currentWindow: true, active: true} , function (tabs){
var activeTab = tabs[0];
chrome.tabs.executeScript(activeTab.id, {file: "content_script.js" }, function (response) {
if (!chrome.runtime.lastError) {
var parsed_res = JSON.parse(response[0]);
console.log("content-script: " + parsed_res);
}
});
});
}
Content-script for this programmatic injection part is equal as the content-script file of my non edited part of the question.
I'm trying to use the webrequest api and I'm having trouble using it to block a website.
manifest.json
{
"manifest_version": 2,
"name": "blocktwitter",
"description": "block twitter",
"version": "1.0",
"permissions": [
"https://ajax.googleapis.com/",
"activeTab",
"storage",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"],
"persistent": true
}
}
background.js:
chrome.webRequest.onBeforeRequest.addListener(
function(details) { return {cancel: true}; },
{urls: ["https://twitter.com"]},
["blocking"]);
I copied the copy + pasted the url from twitter, and copied the code from the docs, but it's still not working. I'm not sure what I'm doing wrong. Help would be appreciated.
You have two problems.
Problem #1: Invalid match pattern
The first problem is that the URL you are passing to chrome.webRequest.onBeforeRequest.addListener() is not a valid match pattern. Match patterns require a path component. If you had looked at the console for your background page/script you would have seen the following error:
_generated_background_page.html:1 Unchecked runtime.lastError while running webRequestInternal.addEventListener: 'https://twitter.com' is not a valid URL pattern.
Your background.js should be something like:
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
console.log('In webRequest');
return {cancel: true};
}, {
urls: ["https://*.twitter.com/*"]
}, ["blocking"]
);
Problem #2: No host permission for twitter.com
You need to have permission for the host that you are blocking. You have to declare the host in your permissions array in manifest.json. Something like:
{
"manifest_version": 2,
"name": "blocktwitter",
"description": "block twitter",
"version": "1.0",
"permissions": [
"https://ajax.googleapis.com/*",
"https://*.twitter.com/*",
"activeTab",
"storage",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"],
"persistent": true
}
}
I have a very simple chrome extension, where I'm trying to pass a message from the background script to the content script. But chrome.runtime is undefined.
Here's literally all the code, as you can see there's almost nothing to it. In the content script, runtime is undefined.
Background Script:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.runtime.sendMessage({action: 'someAction'},
function(response) {
console.log('Response: ', response);
});
});
Content Script:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({
user: 'user'
});
});
manifest.json
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"description": "Some stupid extension",
"browser_action": {
"default_icon": "icons/MyExtensionIcon.png"
},
"icons": {
"48": "icons/MyExtensionIcon.png"
},
"permissions": [
"tabs",
"storage",
"https://*/",
"http://*/"
],
"content_scripts": [
{
"matches": ["*://*.twitter.com/*", "https://twitter.com/*"],
"js": ["js/content.js"]
}
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"web_accessible_resources": [
"js/*",
"css/*"
]
}
Other Info:
Chrome Version 58.0.3029.110 (64-bit)
Installing my extension as an
"Unpacked extension" with developer mode
Ok I figured it out. It's absolutely stupid, but it appears this is simply a Heisenbug. By adding a breakpoint, or debugger statement, it causes that value to be undefined. Maybe a chrome bug?
I swear, every day Chrome feels more, and more like Internet Explorer.
I 'm trying to make my own chrome extension
to block the "seen" and "typing" status of facebook.
But it seems my way doesnt work
Can someone help me find my error?
manifest.json
{
"name": "Block Seen Typing",
"description": "Block Seen",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"webRequest",
"*://facebook.com/*",
"*://www.facebook.com/*",
"webRequestBlocking"
]
}
background.js
chrome.webRequest.onBeforeRequest.addListener(
{
urls: [
"https://www.facebook.com/ajax/messaging/typ.php", "https://www.facebook.com/ajax/mercury/mark_seen.php", "https://www.facebook.com/ajax/mercury/change_read_status.php" // here you put the URL that you want to block.
],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]);
Generaly i want to know how to block any webrequest that i want.
It looks like you forgot to actually include any code for a listener. Try something like this
chrome.webRequest.onBeforeRequest.addListener(function(d){
return {cancel:true};
},{urls:["https://www.facebook.com/ajax/messaging/typ.php",
"https://www.facebook.com/ajax/mercury/mark_seen.php",
"https://www.facebook.com/ajax/mercury/change_read_status.php"]},
["blocking"]);
I'm trying to capture the visible area of a page using chrome.tabs.captureVisibleTab. Here is the code that makes the call:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.name == 'screenshot') {
chrome.tabs.captureVisibleTab(null, null, function(dataUrl) {
sendResponse({ screenshotUrl: dataUrl });
});
}
});
But when I try to capture the tab I get this error:
Unchecked runtime.lastError while running tabs.captureVisibleTab: The 'activeTab' permission is not in effect because this extension has not been in invoked.
Here is my manifest file:
{
"manifest_version": 2,
"name": "Empathy",
"version": "0.1",
"description": "Simulate accessibility issues for websites.",
"browser_action": {
"default_icon": "empathy19.png",
"default_title": "Empathy!"
},
"permissions": [
"activeTab",
"contextMenus",
"desktopCapture",
"tabCapture",
"tts" // Text-to-speech
],
"background": {
"scripts": [
"boot.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"src/helpers.js",
"src/colorblindness.js",
"lib/colorvision.js",
"lib/html2canvas.js"
]
}
]
}
I have active tab permissions
The call is being made from a background script
I'm matching <all_urls>
Why do I get that error?
There are things that talk about <all_urls> as something to match, but what I was missing was the <all_urls> permission. After I added the permission, it worked.