-1

I've made a Chrome extension in which user saves their important links in extension and pastes that link by click on contextmenu of Chrome, but there is a bug: it is not working for Google forms .

screencast

When I do click on submit button, the site is giving me an error that you missed the field.
What's the reason for this bug ?

Full content script code from my extension :

var element = null ;

document.addEventListener("contextmenu", function(event){
    element = event.target;
});

var types = [
    "text",
    "url",
    "search",
    "tel",
    "password",
    "email",
    "number",
    "textarea"
];

function getCaretPosition(element){
    var caretPos = 0;

    /* Chrome  and Firefox support */
    if(!document.selection && $.inArray(element.type, types) >= 0){
        /*  element.selectionStart for type email give error because their is a bug in chrome */
        if( element.type == 'email' || element.type == 'number' ){
            caretPos = 0 ;
        }else{
            caretPos = element.selectionStart;
        }
    }
    else {
        /* IE support */
        if(document.selection){
            element.focus();
            var sel = document.selection.createRange();
            sel.moveStart('character', -element.value.length);
            caretPos = sel.text.length;
        }
    }
    return caretPos;
}

$(document).ready(function (){

    chrome.runtime.onMessage.addListener( function (response , sender , sendResponse) {
        var caretposition = getCaretPosition(element);
        var initvalue = element.value ;
        var first_part = initvalue.substr(0, caretposition);
        var last_part = initvalue.substr(caretposition);
        if(element.type == 'email' || element.type =='number'){
            element.value = response.requested_link + initvalue;
        } else {
            var selected_text = element.value.substring(element.selectionStart, element.selectionEnd);
            if ( selected_text != ''){
                last_part = initvalue.substr(caretposition + selected_text.length);
            }
            element.value = first_part + response.requested_link + last_part;
        }
    });

});
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • 1
    Looks like you reset input just before submiting form. That's how i debug image... We could photoshoped it if you wish but i guess you are looking for relevant code instead. Isn't it?!... – A. Wolff Sep 06 '16 at 09:21
  • @A.Wolff you can see my content script [here .](https://github.com/shuboy2014/Saveit-Pasteit/blob/master/js/content.js) –  Sep 06 '16 at 09:24
  • @A.Wolff I am not doing anything after set value attribute of input field . –  Sep 06 '16 at 09:24
  • and i just want to know what is the reason behind the bug . –  Sep 06 '16 at 09:26
  • 2
    It's highly probable Google uses scriptbot-protection by checking event's [isTrusted](https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted) attribute. Press F12 on the webpage, press Ctrl-Shift-F to open Console Search panel, type `isTrusted` and see if it's being checked (to reformat minified code press `{}` in the bottom left corner of the code panel). If this is the case, AFAIK there's nothing you can do except using an external automation utility. – wOxxOm Sep 06 '16 at 09:30
  • 1
    If you are willing to use a type of Firefox add-on other than WebExtensions, you can send isTrusted events (more effort, but can be done). I only mention it because you have tagged this question with the generic [tag:firefox-addon] tag and not the [tag:firefox-webextensions] tag. – Makyen Sep 06 '16 at 09:42
  • @wOxxOm see what i [get .](https://www.dropbox.com/s/gydblcipb5ozyvd/Screenshot%202016-09-06%2015.26.02.png?dl=0) –  Sep 06 '16 at 09:58
  • 2
    Okay, I see. Then maybe the site checks for *any* keyboard event which you don't send. In that case try sending an Enter key (there are plenty of examples you can find by googling, if needed). – wOxxOm Sep 06 '16 at 10:00
  • @wOxxOm thank you so much , now it's working properly . –  Sep 06 '16 at 12:55

1 Answers1

0

Bug : I am not sending keypress event when my extension pastes something in the input/Textarea field .

Soultion : I solved this bug by using sendkeyevent , you can read about how to trigger sendkey event here . .

Community
  • 1
  • 1