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"]);
Related
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.
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
I am trying to make a Chrome extension to workaround Steam's annoying URL warning page. See this for example: https://steamcommunity.com/linkfilter/?url=http://67.69.104.76:84/marville/photos/planes/comet-162a.jpg
What I have so far works except when clicking a link would launch Chrome, in which case the Steam notice page is displayed.
Here is the page on webrequest: https://developer.chrome.com/extensions/webRequest
Any idea how I might get this to work at launch too?
background.js
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
var url = details.url;
var host = url.substring(43);
return { redirectUrl: host };
},
{
urls: ["*://steamcommunity.com/linkfilter/*"],
types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"]
},
["blocking"]
);
manifest.json
{
"manifest_version": 2,
"name": "Steam Link Filter Redirect",
"description": "Bypasses Steam link filtering.",
"version": "1.1",
"background": {"scripts":["background.js"]},
"permissions": [
"webRequest",
"*://steamcommunity.com/linkfilter/*",
"webRequestBlocking"
],
"icons": {
"16": "icon16.png",
"19": "icon19.png",
"48": "icon48.png",
"128": "icon128.png"
}
}
The problem is, of course, that your code executes too late, after onBeforeRequest stage is done for the opening page. I don't think you can work around that.
You probably need to add a fallback mechanism. I suggest adding a content script that is injected in the linkfilter page and changes the page URL to the required one, for example:
"content_scripts": [ {
"matches": ["*://steamcommunity.com/linkfilter/*"],
"js": ["redirect.js"]
} ],
redirect.js:
location.replace(
decodeURIComponent( location.href.replace(/^.*?\?url=/, "") )
);
(I've made it more robust than .substring(43))
I am wondering if it is possible to show the following context menu item only if it is on specific pages.
I think it has something to do with documentUrlPatterns (Which can be seen here as of typing this) but I am not sure how to implement it with the following code:
manifest.json
{
"name": "App Name",
"version": "1.0",
"manifest_version": 2,
"description": "Description",
"permissions": [
"contextMenus",
"tabs"
],
"background": {
"scripts": ["script.js"]
}
}
script.js
function getword(info,tab) {
chrome.tabs.create({
url: "http://www.google.com/search?q=" + info.selectionText,
})
}
chrome.contextMenus.create({
title: "Search: %s",
contexts:["selection"],
onclick: getword,
});
It would be great if you could provide a demo which will only work on specific sites of your choice (For instance, any directory of Stack Overflow and any directory of Google).
PS. The above code allows users to make a selection on any site and provides a button (In the context menu) which will search for what the user has selected on http://www.google.com/search?q= {Selection}
I have stripped down your code to demonstrate selective context menu option display.
manifest.json
{
"name": "zambrey",
"version": "1.0",
"manifest_version": 2,
"description": "Description",
"permissions": [
"contextMenus"
],
"background": {
"scripts": ["trial.js"]
}
}
trial.js
var showForPages = ["https://www.google.com/","*://github.com/zambrey/*","http://www.nytimes.com/"];
chrome.contextMenus.create({
"title": "zambrey",
"documentUrlPatterns":showForPages
});
Be sure to check out http://developer.chrome.com/extensions/match_patterns.html for more details on url pattern syntax.
Also refer to this http://developer.chrome.com/extensions/examples/api/contextMenus/basic.zip sample for more complex context menus.
I believe you want the "permissions" option in the manifest.json:
"permissions": [
"http://stackoverflow.com/*"
]
http://developer.chrome.com/extensions/declare_permissions.html