how to call function after text is selected - greasemonkey

I want to call function after text is selected in the document. Following code is not working
var showSelWin = document.getElementById('innerwindow');
var txt = ' ';
if (document.getSelection) function(){
txt = document.getSelection();
showSelWin.innerHTML = txt;
document.body.insertBefore(showSelWin, document.body.firstChild);}

The document.getSelection method works differently in Google Chrome, Safari and Internet Explorer than in Firefox and Opera.
It returns a string in Firefox and Opera, and returns a selectionRange object in Google Chrome, Safari and Internet Explorer (the document.getSelection method is identical to the window.getSelection method in Google Chrome, Safari and Internet Explorer).
In Firefox, Opera, Google Chrome, Safari and Internet Explorer from version 9, use the window.getSelection method and the toString method of the selectionRange object returned by the window.getSelection method to get the text content of the selection.
In older Internet Explorer versions, use the createRange method of the selection object and the text property of the TextRange object returned by the createRange method to get the text content of the selection.
a working sample for you: http://jsfiddle.net/uX628/
function GetSelectedText () {
if (document.getSelection) { // all browsers, except IE before version 9
var sel = document.getSelection ();
// sel is a string in Firefox and Opera,
// and a selectionRange object in Google Chrome, Safari and IE from version 9
// the alert method displays the result of the toString method of the passed object
alert (sel);
}
else {
if (document.selection) { // Internet Explorer before version 9
var textRange = document.selection.createRange ();
alert (textRange.text);
}
}
}

Related

How to get the active tab in last focused non-extension window?

I am developing an extension which uses a service worker. The service worker opens a popup window using the usual chrome.windows.create().
During development I have 2 chrome windows open, lets call them A & B. A has tabs of various sites. B has tabs for testing my extension.
Now with a tab in focus in B, I click my extension icon on the toolbar which causes my extension to launch its own window, say C, as discussed above. So there are now in total 3 windows open.
In my extension I want to find the tab id of the active tab in B as that's what was I was focused on when I clicked the extension icon. The following however gives me the tab id of the single tab in C which is my extension's popup
async function getTabId() {
let queryOptions = { active: true, lastFocusedWindow: true };
// `tab` will either be a `tabs.Tab` instance or `undefined`.
let [tab] = await chrome.tabs.query(queryOptions);
console.log(tab)
console.log(tab.title);
return tab.id;
}
Which is fine as I passed lastFocusedWindow: true.
So I pass lastFocusedWindow: false , but that gives me the last focused tab in A!
How do I get the last focused tab in B which was the actual non-extension window in focus before my chrome extension launched its popup?
You can get the active tab before opening the new tab and pass the id via URL:
// background script
(async () => {
const [activeTab] = await chrome.tabs.query({active: true, currentWindow: true});
const params = new URLSearchParams({tabId: activeTab?.id});
const newTab = await chrome.tabs.create({url: 'mypage.html?' + params});
})();
// mypage.js
const tabId = +new URLSearchParams(location.search).get('tabId');

Richtext Field buttons not working in IE

It seems that the certain richtext button just does the field refresh and hence nothing gets changed. So, after selecting the text in the editor if we try to perform any of the below "yellow marked (in the image) " operation it just refreshes and nothing happens.
I am using domino 8.5.3 and I have tried IE 10, 13. Any suggestion, or should I just report it as a bug?
Public Link (for testing): http://teamground.de/test.nsf/home.xsp
Domino 8.5.3 comes with CKEditor 3.2.1.6 http://www-01.ibm.com/support/knowledgecenter/SSVRGU_8.5.3/com.ibm.designer.domino.ui.doc/wpd_whatsnew.html?cp=SSVRGU_8.5.3%2F1. Searching the web I can see lots of issues of versions of CK Editor not working on Internet Explorer 10.
You may need to upgrade Domino to have a version of CKEditor that is supported by Internet Explorer 10. Domino 9.0 FP2 packages a version of CK Editor that supports Internet Explorer 11 http://per.lausten.dk/blog/2014/08/xpages-and-domino-9-0-1-fp2-upgrades-to-ckeditor-and-dojo.html
Thanks to Paul Withers and Per Henrik Lausten. It is partially a duplicate question as marked here.
The solution mentioned in the answer is precisely for IE 10.
However, I faced a problem because of the IE version. I thought I will just document it for others. IE 11.0.9 responds "unknown" and hence it doesn't change the response header. I designed a small function for later versions of IE. Hopefully this could help anyone:
function isIE() {
var isBrowserIE = false;
if (context.getUserAgent().getBrowser() == 'Unknown') {
var userAgent = context.getUserAgent().getUserAgent();
if (userAgent.indexOf('Trident/7') > -1 && userAgent.indexOf('rv:11') > -1) {
isBrowserIE = true;
}
}
if (context.getUserAgent().getBrowser() == 'Microsoft Internet Explorer') {
isBrowserIE = true;
}
if (context.getUserAgent().getBrowser() == 'IE') {
isBrowserIE = true;
}
return isBrowserIE;
}
And then as suggested by Per Henrik Lausten we could render the response header to IE 9 (would just copy it for one click answer)
<xp:this.beforeRenderResponse><![CDATA[#{javascript:
if (isIE()) {
var response = facesContext.getExternalContext().getResponse();
// Use IE9 mode because of CKEditor bugs with IE10
response.setHeader("X-UA-Compatible", "IE=9");
}
}]]></xp:this.beforeRenderResponse>

Orchard Module only shows Media Library items to logged in users

I created a very simple module that displays images in a specific folder in the Media Library. It works great when I'm logged in to my Orchard site, but the images are not displayed to anonymous users. Here is a simplified version of my module code:
public ActionResult Image(int id, int? width, int? height) {
var items = _mediaLibraryService
.GetMediaContentItems("Portfolio", 0, Int32.MaxValue, null, null);
var mediaItem = items.Where(i => i.Id == id).SingleOrDefault();
if (mediaItem == null) {
return null;
}
string imageFileName = Server.MapPath(mediaItem.MediaUrl);
string contentType = "image/" + Path.GetExtension(imageFileName).Substring(1);
// code to resize image based on given parameters ...
return File(imageFileName, contentType);
}
The action accepts the MediaPart id and dimensions, finds the item by id, resizes the image, and returns it as a FileResult. This works fine when logged in, but anonymous users get nothing. Looking in the network tab in Chrome, it shows that the response length is 0.
Of course, I don't get this behavior when testing locally, only on my live site. Any ideas?

Calling HtmlPage.PopupWindow from ChildWindow.Closed event

Context:
Silverlight 5, 64 bit, Win7, VS2010
Expected: a new Browser Window/Tab opens up for the url passed in.
Actual: no new browser window ot tab appears.
Code Sample: (called from ViewModel. The View is a sdk:ChildWindow.
AddRelatedIssueDialog dialog = new AddRelatedIssueDialog();
dialog.Closed +=
(s1, e1) =>
{
if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
{
var window = HtmlPage.PopupWindow("www.goole.com", "_blank", null);
}
};
dialog.Show();
We have also tried Prism events to do the same thing but with no joy...
Also have included the on the control host - but again no luck.
Any help much appreciated:
Cheers,
John
Try this:
HtmlPage.Window.Navigate(new Uri("www.google.com"), "_blank");
It works for me :)

Generate Screenshot From WebBrowser Control

I grabbed some code from the link below to generate a screenshot from a web browser control:
http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx
Then, I modified to be an extension method like so:
public static Bitmap GetScreenshot(this WebBrowser webBrowser)
{
// Load the webpage into a WebBrowser control
using (WebBrowser wb = new WebBrowser())
{
wb.ScrollBarsEnabled = false;
wb.ScriptErrorsSuppressed = true;
wb.Navigate(webBrowser.Url);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
// Set the size of the WebBrowser control
wb.Width = wb.Document.Body.ScrollRectangle.Width;
wb.Height = wb.Document.Body.ScrollRectangle.Height;
// Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
return bitmap;
}
}
It works great for most web pages, but I am trying to use it for a SharePoint site and the image is coming out as 250x250 every time. :-s
Any geniuses here that can give me a solution?
Thanks in advance
Well, the reason for this seems to be that my new WebBrowser (wb) is uninitialized even after I call Navigate(). The Url property shows as null. This is weird. Does it mean I cannot create a WebBrowser in code and never display it? It would be a shame to have to display the thing. :-(
You can check that question taking-screenshot-of-a-webpage-programmatically.
It is based on this webpage_thumbnailer which uses Web Browser control.
For one, the DrawToBitmap class does not exist under the webbrowser control. I suggest using CopyFromScreen. Here is a code snippet:
Bitmap b = new Bitmap(wb.ClientSize.Width, webBrowser1.ClientSize.Height);
Graphics g = Graphics.FromImage(b);
g.CopyFromScreen(this.PointToScreen(wb.Location), new Point(0, 0), wb.ClientSize);
//The bitmap is ready. Do whatever you please with it!
b.Save(#"c:\screenshot.jpg", ImageFormat.Jpeg);
MessageBox.Show("Screen Shot Saved!");

Resources