How to get page status with firefox extension? - firefox-addon

I want to get page status codes like 40* or 30*. Firefox extension can get status when page loaded, when content_script.js executed. But content script not executed when page not loaded (3xx or 4xx errors).
How to check that the tab with a specific URL is not loaded?

To do this you need to handle the onHeadersReceived event in your background.js. You can refer the documentation here.
browser.webRequest.onHeadersReceived.addListener(function(details){
if(!details.initiator)
{
alert(details.url+" "+details.statusCode);
}
},
{
urls: ["<all_urls>"]
},
["responseHeaders", "blocking"]);
In your manifest.json you need to add
"permissions": ["webRequest","webRequestBlocking","<all_urls>"],

Related

Create simple Chrome extension - single click forward tab url to https

trying to create a extremely simple chrome extension, but need some help.
Purpose: When I click the extension icon, URL of existing tab must be forwarded to telegram. This can be done via a HTTPS call like https://api.telegram.org/bot123456:xxxxxxxxxxxxxxxxxxxxxx/sendMessage?chat_id=12345678&text=https://activetab.url
Single extension button, when clicked:
Grab URL of active link
Post http enriched with link url grabbed in previous step
No security concerns, I want to install the extension in developer mode only in my own browser.
Goldplated option would be to let the user specify the bot, the token & recipient id in settings of the app.
Thx for any feedback
It should only take a few lines of code.
I assumed that the http request is of type: "GET".
If not, you have to modify the fetch command according to Telegram specifications.
//manifest.json
{
...
"background": {
"service_worker": "background.js"
},
"action": {
"default_icon": { // optional
"16": "images/icon16.png", // optional
...
},
"default_title": "Click Me" // optional
},
"permissions": ["tabs"]
...
}
//background.js
chrome.action.onClick.addListener(t => fetch('https://api.telegram.org/bot123456:xxxxxxxxxxxxxxxxxxxxxx/sendMessage?chat_id=12345678&text=' + tab.url))

Custom protocol for Docusaurus

We want to serve a Docusaurus-build with Electron.
For this we are using a custom protocol that just serves the files to the Electron-Browser.
The Problem is, the Javascript that is running in the static-html build of the Docusaurus app just wont accept the url (at least that is what we think)
If we serve "doc://doc/docs/intro/index.html" it pops up for a second and afterwards the "Page Not Found"-page is shown - because the javascript does that.
Our url is "doc://doc" and our baseUrl is "/" and we can not figure out how to stop the Javascript from chaning the currently loaded page to the Page not found one.
(We disabled the Javascript and if it is disabled that error does not appear)
The problem was Electron...
You have to give the custom protocol privledge with this:
protocol.registerSchemesAsPrivileged([
{
scheme: 'doc',
privileges: { secure: true, standard: true },
},
]);
So it has actually nothing to do with Docusaurus.

Firefox addon opens tab with content, copy/paste of the tabs url will not load page fully

I have an addon that places an ActionButton on the toolbar. When the ActionButton is clicked the code below is run.
The code opens a new tab and provides some html and js, this acts as the addon's UI.
The url of the new tab is:
resource://jid1-qljswfs6someid-at-jetpack/addon-firefox/data/html/view.html
If I copy/paste that url into another new tab manually, the html displays but the js logic is not loaded. Is there a way to do this without clicking the ActionButton? So I could maybe bookmark the addon instead of having the ActionButton take up space.
Code:
Tabs.open({
url: require("sdk/self").data.url('html/view.html'),
onReady: function onReady(tab) {
worker = tab.attach({
contentScriptFile: [
require("sdk/self").data.url.get('lib/lib1.js'),
require("sdk/self").data.url.get('js/lib1.js')
],
onMessage: function(message) {
console.log('stuff done');
}
});
}
});
In order to run it whenever the site from data.url('html/view.html') is loaded you would have to use page-mod instead of manually attaching to the document to the tab.
Your include pattern would be something like data.url('html/view.html') + "*", so it also attaches to the page if there is a hash or a query to the document.

how to load Jquery tab content everytimg it is clicked?

I have 3 links made as tabs in my jsp page. Every link goes into a servlet and fetches the data. However when i try the same in IE-7, I don't see that tab content is getting loaded every-time i clicked the tab.
When i try the same page in Firefox i see that page is getting dynamically loaded every time i click the tab. Can someone please help me how to make it work on IE. All these links get data from 3 different servlets Thank you!
I am using jquery-1.7.2, jquery-ui-1.8.22 and using the Le_Frog theme.
My tab code:
$("#main").tabs({
cache: false,
spinner: "Processing"
});
I added the following code and it worked fine.
$("#main").tabs({
cache: false,
spinner: "Retrieving data...",
ajaxOptions: { async: false,cache:false }
});

Jquery mobile page navigation corrupts base url and causes ajax on main page to fail

My main page is here:
http://www.mydomain.com/main/main.php
My login page is here:
http://www.mydomain.com/main/pages/login.php
Main.php uses ajax to fetch data in response to a tap event. This works fine until I navigate to my login page and then back to my main page. After going to the login page and back, the relative paths get messed up such that the ajax looks for server file in the wrong place.
here is the ajax:
1. function get_more_data() {
2. more_data_index += 15;
3. var formData = "index=" + more_data_index;
4. $.ajax({
5. type: "POST",
6. url: "genxml.php", // file located here: http://www.mydomain.com/main/genxml.php
7. cache: false,
8. data: formData,
9. dataType: "xml",
10. success: showFiles3,
11. error: onErrorMoreData
12. });
13. }
After I navigate back to main.php from login.php the ajax tries posting to the wrong location:
http://www.mydomain.com/main/pages/genxml.php
(genxml.php is not in the "pages" subdirectory; it's in the main directory.)
I tried updating the ajax to use an absolute path:
url: "http://www.mydomain.com/main/genxml.php"
This made the post successful, but my data parsing failed because relatives paths are used in the main file for things like images. So instead of getting images from here: http://www.mydomain.com/main/ the script was trying to get images from here: http://www.mydomain.com/main/pages/
I've found a few posts with people having similar issues, but I've not come across a solution. I've also tried reading the jquerymobile docs and it's very possible that the jquery developers attempt to cover this issue here, but I admit I don't completely understand everything on this page:
http://jquerymobile.com/demos/1.0b3/#/demos/1.0b3/docs/pages/page-navmodel.html
If anyone can help I would really appreciate it. Thanks.
P.S. This issue happens on Android and Google Chrome, but not in Firefox.
I have created a working example of what you're trying to do. You should be able to look at this and see what I've done. Be sure to checkout the master.js. I think that the key to making it work in your situation is to nest the ajax calls within the "pageshow" event to be sure that your baseURL has been updated. You can download the example at http://www.roughlybrilliant.com/stackoverflow/7372909.7z
View the example in action as it pulls in weather.xml with relative URLs.
$("div").live("pageshow", function(){
var $page = $(this);
get_more_data();
});
Why don't you use one of this:
use "../main.php" when redirecting back from login page, or
remember UrlRefer from Headers when you entering login.php and use that to redirect back to any previous page with 301

Resources